Maven插件从pluginManagement继承

3

我有一个父pom.xml文件,其中定义了Maven-ear-plugin的默认配置。

<pluginManagement>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-ear-plugin</artifactId>
            <version>2.8</version>
            <configuration>
                <modules>
                    <jarModule>
                        <groupId>com.test</groupId>
                        <artifactId>artifact1</artifactId>
                    </jarModule>
                    <jarModule>
                        <groupId>com.test</groupId>
                        <artifactId>artifact2</artifactId>
                    </jarModule>
                </modules>
            </configuration>
        </plugin>
    </plugins>
</pluginManagement>

在子POM中,我在<plugins>部分定义了maven-ear-plugin。
<plugin>
    <artifactId>maven-ear-plugin</artifactId>
    <executions>
        <execution>
            <id>default-ear</id>
            <phase>package</phase>
            <goals>
                <goal>ear</goal>
            </goals>
            <configuration>
                <earSourceDirectory>EarContent</earSourceDirectory>
                <defaultLibBundleDir>APP-INF/lib</defaultLibBundleDir>
                <modules>
                    <jarModule>
                        <groupId>com.test</groupId>
                        <artifactId>artifact3</artifactId>
                    </jarModule>
                </modules>
            </configuration>
        </execution>
    </executions>
</plugin>

我的意图是将artifact1、artifact2和artifact3这三个jar文件全部包含在ear中。然而,在构建后,只有在子pom.xml中定义的artifact3被包含了进去。因此,看起来默认情况下子pom.xml中的<modules>定义会覆盖父pom中的定义,而不是将它们合并在一起。事实上,如果我删除子pom.xml中整个<modules>部分,构建后artifact1和artifact2就会被包含进去。
我的问题是是否有一种方法可以同时包含父pom和子pom中定义的所有jar模块。我有几个ear项目,它们都需要包含相同的一组jar模块以及自己的一些jar模块。我试图将公共的jar模块移动到父pom中,这样它们就不会在每个子pom.xml中重复出现。
更新以澄清我的项目关系:
parent pom.xml (define maven-ear-plugin in the pluginManagement section)
  -- project1 pom.xml (multi-module project)
      -- myJar-proj1 
      -- myWeb-proj1
      -- myEar-proj1 (define maven-ear-plugin in the <build> section)    
  -- project2 pom.xml (multi-module project)
      -- myJar-proj2
      -- myWeb-proj2
      -- myEar-proj2 (define maven-ear-plugin in the <build> section)

我假设您已经直接在build下定义了插件,而不是在build下的pluginManagement中定义。这个说法正确吗?- 谢谢 - saurav
1个回答

7
为了实现合并行为,您应该将maven-ear-plugin 放在父pomplugins标签下(而不是放在pluginManagement)。
pom
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-ear-plugin</artifactId>
            <version>2.8</version>
            <configuration>
                <modules>
                    <jarModule>
                        <groupId>com.test</groupId>
                        <artifactId>artifact1</artifactId>
                    </jarModule>
                    <jarModule>
                        <groupId>com.test</groupId>
                        <artifactId>artifact2</artifactId>
                    </jarModule>
                </modules>
            </configuration>
        </plugin>
    </plugins>
</build>

编辑

在 OP 澄清项目结构后:在子项目中的 modules 元素上,属性 combine.children="append"

<modules combine.children="append">
    <jarModule>
        <groupId>com.test</groupId>
        <artifactId>artifact3</artifactId>
    </jarModule>
</modules>

父级项目应该仅在pluginManagement中定义插件。

我不能这样做。如果我这样做,所有的子项目都将启用maven-ear-plugin插件。我有一个经典的多模块maven项目设置。 ear项目只是其中一个子项目。还有jar / war项目是ear项目的同层级。 - Lan
@Lan,你不应该在所有项目的父级中定义maven-ear-plugin,而是只在作为所有 ear 项目的父级的子 ear 项目中定义。 - yair
我更新了我的问题以澄清我的项目关系。我认为你建议在project1和project2的pom.xml文件中,在<build>下定义maven-ear-plugin。我看到两个缺点。首先,每个maven-ear-plugin定义都必须列出所有的jar模块。我更喜欢在顶层父级pom.xml中定义通用的jar模块。其次,如果我在project1和project2的构建部分中定义maven-ear-plugin,所有子项目(例如myJar-proj1、myWeb-proj2)都将具有maven-ear-plugin。这不是我想要的。我们只希望在myEar-proj1和myEar-proj2中使用maven-ear-plugin。 - Lan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接