Execute Maven plugin goal on child modules, but not on parent

JavaMaven 2

Java Problem Overview


In a multi-module project, how can you specify that you want to execute a plugin goal in all the child-modules, but not on the parent project? There is <pluginManagement>, but that only defines the configuration for the execution -- the child modules would still need to reference the plugin to get the goal executed:

> [...] However, this only configures plugins that are actually referenced within the plugins element in the children. (POM Reference)

Any other way to achieve this?

UPDATE: I've tried this according to Pascal's advice:

<!-- ... -->
<packaging>pom</packaging>
<modules>
  <module>child</module>
</modules>

<build>
  <plugins>
    <plugin>
      <artifactId>maven-jar-plugin</artifactId>
      <executions>
        <execution>
        <phase>integration-test</phase>
        <goals>
          <goal>jar</goal>
        </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>
<!-- ... -->

This will still generate a .jar for the parent project, even though the jar goal is bound to the integration-test phase.

Java Solutions


Solution 1 - Java

According to the Default Lifecycle Bindings, the bindings for a packaging pom are:

> Default Lifecycle Bindings - Packaging > pom > >

> package       site:attach-descriptor
> install install:install
> deploy deploy:deploy >

So if your parent POM has a <packaging>pom<packaging> (this should be the case as pointed out in a comment) and if you bind your plugins to other phases than those above (see the Lifecycle Reference for a comprehensive list), they won't be executed during the build of the parent POM.

(EDIT: My initial answer is just wrong. If you bind a plugin goal to a particular phase, it will be triggered during that phase, regardless of the packaging of the project. The Default Lifecycle Bindings don't have anything to do with that, they are just default lifecycle bindings. All what matters is if the phase to which the plugin is bound is part of the build lifecyle.)

As you pointed out, you can use the pluginManagement in the parent pom for the configuration of the plugin but if you really want to execute a plugin goal in children modules and not in the parent (you might have good reasons to do this but most of time, plugins won't have much effet on a module with a pom packaging that doesn't have any content), you'll have to reference plugins in the plugins element in the children.

Applied to your example, the parent pom.xml could define the following specifications:

<project>
  <packaging>pom</packaging>
  ...
  <modules>
    <module>child</module>
  </modules>
  ...
  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-jar-plugin</artifactId>
          <version>2.2</version>
          <executions>
            <execution>
              <id>my-execution-id</id>
              <phase>integration-test</phase>
              <goals>
                <goal>jar</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
        ...
      </plugins>
    </pluginManagement>
  </build>
  ...
</project>

And in every child pom.xml, only the following is required:

<project>
  ...
  <build>
    ...
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
      </plugin>
    </plugins>
    ...
  </build>
</project>

Solution 2 - Java

The described solution with plugin management is certainly correct, but in certain cases it does not fit. Suppose you would like to run several jar:jar goals in the child module each configured with its own settings (configuration) per execution. Or in general, when you don't want to force child poms to explicitly trigger the plugin(s).

In this case the solution that worked for me was to define the executions in the parent pom under a specific profile, and have it activated only in child poms for example by checking for existence of some file or property:

<profile>
    <id>generate-dc</id>
    <activation>
        <file>
            <exists>src/main/assembly/some.xml</exists>
        </file>
    </activation>

Then plugins won't be executed in the parent, but will be executed in all children if those contain the file, or set some property.

Solution 3 - Java

I had a similar requirement to run some plugins in the child but not the parent POM. i achieved this by stating <skip>true</skip> in the parent POM.

The parent pom entry is below:

<plugin>
    <groupId>eviware</groupId>
    <artifactId>maven-soapui-plugin</artifactId>
    <version>4.0.0</version>
    <inherited>false</inherited>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
		    <version>4.8.2</version>
        </dependency>
    </dependencies> 
    <configuration>
        <skip>true</skip>
    </configuration>
</plugin> 

The child project pom entry is below

<plugins>
    <plugin>
        <groupId>eviware</groupId>
        <artifactId>maven-soapui-plugin</artifactId>
        <version>4.0.0</version>
        <configuration>
            <settingsFile>site-service-web/src/test/soapui/soapui-settings.xml</settingsFile>
            <projectFile>site-service-web/src/test/soapui/PodifiSite-soapui-project.xml</projectFile>
            <outputFolder>site-service-web/target/surefire-reports</outputFolder>
            <junitReport>true</junitReport>
            <exportwAll>true</exportwAll>
            <printReport>true</printReport>
        </configuration>
    </plugin>
</plugins>

Solution 4 - Java

This below config worked for me. Add the plugin in both the parent and child pom.

Parent :

<build>
  <plugins>
    <plugin>
      <artifactId>maven-jar-plugin</artifactId>
      <inherited>true</inherited>
      <executions>
        <execution>
        <phase>integration-test</phase>
        <goals>
          <goal>jar</goal>
        </goals>
        </execution>
      </executions>
      <configuration>
         <skip>true</skip>
      </configuration>
    </plugin>
  </plugins>
</build>

Child

<build>
  <plugins>
    <plugin>
      <artifactId>maven-jar-plugin</artifactId>
      <inherited>false</inherited>
      <executions>
        <execution>
        <phase>integration-test</phase>
        <goals>
          <goal>jar</goal>
        </goals>
        </execution>
      </executions>
      <configuration>
         <skip>false</skip>
      </configuration>
    </plugin>
  </plugins>
</build>

Solution 5 - Java

I tried the answer from Pascal but it did not work for me. The plugins referenced in the child pom did not execute, I'm assuming because they did not have a build phase binding.

The post here describes a solution that works by binding the plugins to execution ids and build phases: https://stackoverflow.com/questions/13250377/how-to-override-default-binding-to-phase-of-a-maven-plugin

I'd recommend that to anyone else trying to get this working.

Solution 6 - Java

Use <inherited>false</inherited> under the plugins section in the parent project.

Plese refer to this page for more information.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionL&#243;r&#225;nt Pint&#233;rView Question on Stackoverflow
Solution 1 - JavaPascal ThiventView Answer on Stackoverflow
Solution 2 - JavaMikhailView Answer on Stackoverflow
Solution 3 - JavaRafeeqView Answer on Stackoverflow
Solution 4 - JavalakshmanasView Answer on Stackoverflow
Solution 5 - JavaMichael SiegelView Answer on Stackoverflow
Solution 6 - JavaY LView Answer on Stackoverflow