Maven2:如何在父POM和子POM之间共享插件配置?

15

我试图减少我们的Maven POM文件中的复制粘贴。

我们有一个主POM和许多从主POM继承的子项目POM。

我想共享一个类似于以下内容的复杂插件定义:

<plugins>
    ...
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>appassembler-maven-plugin</artifactId>
        <configuration>
            <!-- many xml lines here --> 
        </configuration>

        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>assemble</goal>
                    <goal>generate-daemons</goal>
                    <goal>create-repository</goal>
                </goals>
            </execution>
        </executions>

        <dependencies>
            <dependency>
                <groupId>org.codehaus.mojo.appassembler</groupId>
                <artifactId>appassembler-booter</artifactId>
                <version>1.0</version>
            </dependency>
        </dependencies>
    </plugin>
    ...
</plugins>

如果将此插件定义放在项目pom中,打包可以正常进行。
当将定义移动到父pom中(在<dependencyManagement><plugins>中),则甚至未开始打包。

是否可以共享插件配置?如何实现?

-- 在第一次得到答案后进行编辑 ---
我已经尝试了以下操作:
- 将我的XL打包插件配置放在父pom的<plugins>元素中
- 在项目pom的<build><plugins>元素中添加这些行:

<plugins>
...
   <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>appassembler-maven-plugin</artifactId>
   </plugin>
...
</plugins>

但是它没有起作用...可能有什么问题?

-- 最后编辑 --
我想我明白了问题所在:
插件重复使用声明应该在配置文件构建中声明。
我在一个始终启用的插件中完成了这个步骤,现在它可以正常工作了。

非常感谢。

2个回答

27

你可以使用<pluginManagement>标签来包装父级的插件。

   <build> 
       <pluginManagement>
           <plugins>
               <plugin> ... </plugin>
           </plugins>
       </pluginManagement>
   </build>

当子插件在其构建标签中声明插件时,它们将继承配置。


我已经尝试过这个,但它没有起作用。我已经更新了我的问题,关于你的答案。 - Guillaume
1
运行 help:effective-pom 命令,查看插件配置的具体情况。你肯定是在使用 pluginManagenement,对吧? - John Vint
使用插件构建时已在配置文件中声明。因此,插件配置也应该在配置文件中声明... 感谢您的帮助。 - Guillaume

2

你尝试过使用Maven的插件管理功能吗?凭借它,你可以将配置信息从父pom.xml文件推送到子pom.xml文件中:

   <build> 
       <pluginManagement>
           <plugins>
               <plugin>your_plugin</plugin>
           </plugins>
       </pluginManagement>
   </build>

现在,并不是所有的插件都像org.apache.maven.plugins组的那些一样做得很好。可能需要将您的配置部分移动到执行元素之间。


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