Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

maven

maven-3

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.

like image 246
bacar Avatar asked May 14 '13 10:05

bacar


People also ask

What is the difference between plugin and pluginManagement tags in Maven?

pluginManagement: is an element that is seen along side plugins. Plugin Management contains plugin elements in much the same way, except that rather than configuring plugin information for this particular project build, it is intended to configure project builds that inherit from this one.

Are Maven plugins executed in order?

Plugin executions are ordered according to their phases. See https://maven.apache.org/ref/current/maven-core/lifecycles.html for the order of phases. For example, here mavin-plugin-1 is executed before maven-plugin-2 because the process-resources phase is defined as taking place before the compile phase.

Does Maven allow third party plugins?

However, as previously mentioned, the user may have a need for third-party plugins. Since the Maven project is assumed to have control over the default plugin groupId, this means configuring Maven to search other groupId locations for plugin-prefix mappings. As it turns out, this is simple.


1 Answers

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.

like image 84
user944849 Avatar answered Sep 28 '22 04:09

user944849