Execute Maven plugin goal on parent module, but not on children

走远了吗. 提交于 2019-11-28 04:49:48

As documented in the Plugins section of the pom reference:

Beyond the standard coordinate of groupId:artifactId:version, there are elements which configure the plugin or this builds interaction with it.

  • inherited: true or false, whether or not this plugin configuration should apply to POMs which inherit from this one.

So just add <inherited>false</inherited> to the buildnumber-maven-plugin configuration to avoid inheritance in children POMs:

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

You can add <inherited>false</inherited> to the plugin configuration to avoid inheritance in children POMs:

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

Or, if your plugin has multiple executions, you can control which executions are inherited and which are not by adding the inherited tag to the execution body:

<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>

There is a built-in maven option: mvn --help ... -N,--non-recursive Do not recurse into sub-projects

Just an addition to the great answers here: note that per-execution inheritance is broken in Maven 2: http://jira.codehaus.org/browse/MNG-3959

If the plugin is custom one and you have access to plugin MOJO code, you can mark the plugin as aggregator; if the expected behavior is applicable for all projects where plugin is to be used.

As mentioned in Mojo API Specification ,

Flags this Mojo to run it in a multi module way, i.e. aggregate the build with the set of projects listed as modules.

Example,

@Mojo(name = "createHF", inheritByDefault = false, aggregator = true)
public class CreateHFMojo extends AbstractMojo {

..

public void execute() throws MojoExecutionException, MojoFailureException {
 ....
}

..

}

Detailed example on github.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!