Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
381 views
in Technique[技术] by (71.8m points)

maven - Can I configure multiple plugin executions in pluginManagement, and choose from them in my child POM?

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.

question from:https://stackoverflow.com/questions/16540808/can-i-configure-multiple-plugin-executions-in-pluginmanagement-and-choose-from

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...