在父模块上执行Maven插件目标,但不在子项上

我们有一个多模块的maven项目,它使用一个configuration文件来定义一个buildnumber-maven-plugin来增加一个内部版本号,然后将其检查到源代码控制中。

如果我在父pom.xml中定义插件,它也会执行所有子构build。

这是我的父pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.webwars</groupId> <artifactId>parent</artifactId> <packaging>pom</packaging> <properties> <buildNumber.properties>${basedir}/../parent/buildNumber.properties</buildNumber.properties> </properties> <version>1.0-SNAPSHOT</version> <name>Parent Project</name> <profiles> <profile> <id>release</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <debug>false</debug> <optimize>true</optimize> </configuration> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>buildnumber-maven-plugin</artifactId> <version>1.0-beta-3</version> <executions> <execution> <phase>validate</phase> <goals> <goal>create</goal> </goals> </execution> </executions> <configuration> <buildNumberPropertiesFileLocation>${buildNumber.properties}</buildNumberPropertiesFileLocation> <getRevisionOnlyOnce>true</getRevisionOnlyOnce> <doCheck>false</doCheck> <doUpdate>false</doUpdate> <format>{0, number}</format> <items> <item>buildNumber</item> </items> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-scm-plugin</artifactId> <executions> <execution> <phase>install</phase> <goals> <goal>checkin</goal> </goals> </execution> </executions> <configuration> <basedir>${basedir}</basedir> <includes>buildNumber.properties</includes> <message>[Automated checkin] of ${basedir} Build version: ${major.version}.${minor.version}.${buildNumber}</message> <developerConnectionUrl>...</developerConnectionUrl> </configuration> </plugin> </plugins> </build> </profile> </profiles> <modules> <module>../common</module> <module>../data</module> <module>../client</module> <module>../webplatform</module> </modules> ... </project> 

正如pom参考的插件部分所述:

除了groupId的标准坐标:artifactId:version之外,还有一些configuration插件的元素,或者是与它build立交互。

  • inheritance: true或false,这个插件configuration是否应该适用于从这个inheritance的POM。

因此,只需将<inherited>false</inherited>到buildnumber-maven-pluginconfiguration,以避免在儿童中inheritancePOM:

  <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>buildnumber-maven-plugin</artifactId> <version>1.0-beta-3</version> <inherited>false</inherited> ... </plugin> 

您可以将<inherited>false</inherited>到插件configuration中,以避免在儿童中inheritancePOM:

  <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>buildnumber-maven-plugin</artifactId> <version>1.0-beta-3</version> <inherited>false</inherited> ... </plugin> 

或者,如果您的插件有多个执行,您可以控制哪些执行被inheritance,哪些不是通过将inheritance标签添加到执行体:

 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <executions> <execution> <id>parent-only</id> <phase>initialize</phase> <inherited>false</inherited> <configuration> <target> <echo message="Echoed only by this module."/> </target> </configuration> <goals> <goal>run</goal> </goals> </execution> <execution> <id>all-modules</id> <phase>initialize</phase> <inherited>true</inherited> <!-- Defaults to true, so you could leave this line out --> <configuration> <target> <echo message="Echoed in this module and each child module."/> </target> </configuration> <goals> <goal>run</goal> </goals> </execution> </executions> </plugin> 

除了这里的优秀答案之外:请注意,在Maven 2中,每个执行的inheritance都被破坏了: http : //jira.codehaus.org/browse/MNG-3959

有一个内置的maven选项: mvn --help ... -N,--non-recursive Do not recurse into sub-projects

如果插件是自定义插件,并且您可以访问插件MOJO代码,则可以将该插件标记为aggregator ; 如果预期行为适用于所有使用插件的项目。

正如在Mojo API规范中所提到的,

标记这个Mojo以多模块的方式运行它,也就是将构build与列为模块的项目集合在一起。

例,

 @Mojo(name = "createHF", inheritByDefault = false, aggregator = true) public class CreateHFMojo extends AbstractMojo { .. public void execute() throws MojoExecutionException, MojoFailureException { .... } .. } 

在github上的详细例子。