45

I have 2 common plugin-driven tasks that I want to execute in my projects. Because they're common, I want to move their configuration to the pluginMangement section of a shared parent POM. However, both of the 2 tasks, whilst otherwise completely distinct, use the same plugin. In some of my projects I only want to do 1 of the 2 tasks (I don't always want to run all executions of the plugin).

Is there a way to specify multiple different executions of a plugin, within the pluginManagement section of a parent pom, and choose in my child pom one (and only one) of those executions to actually run? If I configure 2 executions in pluginManagement, it seems that both executions will run.

Note: I think this may, or may not, be a duplicate of question Maven2 - problem with pluginManagement and parent-child relationship, but as the question is nearly 4 screenfuls long (TL;DR), a succinct duplicate might be worthwhile.

Community
  • 1
  • 1
bacar
  • 9,761
  • 11
  • 55
  • 75
  • If you want to share only configuration, why not put only configuration in pluginManagement, and executions in children ? – Tristan Feb 05 '21 at 15:13

1 Answers1

68

You're correct, by default Maven will include all of the executions you have configured. Here's how I've dealt with that situation before.

<pluginManagement>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>some-maven-plugin</artifactId>
    <version>1.0</version>
    <executions>
      <execution>
        <id>first-execution</id>
        <phase>none</phase>
        <goals>
           <goal>some-goal</goal>
        </goals>
        <configuration>
          <!-- plugin config to share -->
        </configuration>
      </execution>
      <execution>
        <id>second-execution</id>
        <phase>none</phase>
        <goals>
           <goal>other-goal</goal>
        </goals>
        <configuration>
          <!-- plugin config to share -->
        </configuration>
      </execution>
    </executions>
  </plugin>
</pluginManagement>

Note, the executions are bound to phase none. In the child, you enable the parts that should execute like this:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>some-maven-plugin</artifactId>
    <executions>
      <execution>
        <id>first-execution</id>         <!-- be sure to use ID from parent -->
        <phase>prepare-package</phase>   <!-- whatever phase is desired -->
      </execution>
      <!-- enable other executions here - or don't -->
    </executions>
</plugin>

If the child doesn't explicitly bind the execution to a phase, it won't run. This allows you to pick and choose the executions desired.

user944849
  • 14,524
  • 2
  • 61
  • 83
  • Going to try this when I can, but do you know if this would also run any other executions from the parent which do not have the phase set to `none`? – bacar May 14 '13 at 14:57
  • If you have `first-execution` bound to, say, 'generate-resources' phase and `second-execution` bound to 'none', then reference this plugin in the child, `first-execution` would always run. You'd need to manually enable `second-execution` with the explicit phase binding I described. Run Maven with -X should show the plugin executions and order (particularly if you are using Maven 3, I prefer its log format). – user944849 May 14 '13 at 15:10
  • OK; it seems that even if I manually specify only `second-execution`, `first-execution` will always run (if it is not bound to `none`); i.e. I have no way of suppressing an execution defined in a parent _without_ settings its phase to `none` (and adding all the extra verbose XML to set it back to something meaningful again in the child). Still, this seems to be the best solution I've seen with the least boilerplate - thanks! – bacar May 14 '13 at 16:44
  • In case anybody else gets thrown by this - some versions (<1.3) of the m2e plugin for eclipse don't like this syntax (despite it working fine from maven). Upgrade! – bacar May 17 '13 at 07:51