Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute a maven plugin twice with different property

Tags:

I would like to build from a maven pom running two sequential executions of the same plugin, in the same phase differing only by a single property, which will result in two different archives being created. Since the configuration is rather complicated, I'd rather NOT copy it just to change one value, which would create a maintenance nightmare. If it was somehow possible to define such a property in the <executions> section of the plugin config, I could avoid this headache.

Question: Is this possible and if so how?

Update: Two answers have mentioned using multiple executions and one of them mentions that you can have separate configurations in each execution. But given that the majority of my configuration is constant between the two executions, can I have one configuration on the plugin level and also have configuration sections in each execution for the parts that are different?

like image 325
Steve Cohen Avatar asked Dec 09 '15 21:12

Steve Cohen


People also ask

How to configure Maven plugin in POM XML?

Next, open the command console and go to the folder containing pom. xml and execute the following mvn command. Maven will start processing and displaying the clean phase of clean life cycle. Plugins are specified in pom.

Are Maven plugins reusable?

Almost any action that you can think of performing on a project is implemented as a Maven plugin. Plugins are the central feature of Maven that allow for the reuse of common build logic across multiple projects.

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.


1 Answers

Given the simple Maven Source Plugin configuration (as an example) you have a shared configuration across all of its executions (outside the executions element) and then a custom configuration per each execution, for the same phase, as requested by your question:

<build>     <plugins>         <plugin>             <groupId>org.apache.maven.plugins</groupId>             <artifactId>maven-source-plugin</artifactId>             <version>2.4</version>             <configuration>                 <includePom>true</includePom>             </configuration>             <executions>                 <execution>                     <id>test-id1</id>                     <phase>verify</phase>                     <goals>                         <goal>jar</goal>                     </goals>                     <configuration>                         <finalName>aaa</finalName>                     </configuration>                 </execution>                 <execution>                     <id>test-id2</id>                     <phase>verify</phase>                     <goals>                         <goal>jar</goal>                     </goals>                     <configuration>                         <finalName>bbb</finalName>                     </configuration>                 </execution>             </executions>         </plugin>     </plugins> </build> 

The configuration entry <includePom>true</includePom> will in this case be merged with the custom configurations of each execution and as such centralize the common configuration as plugin generic configuration.

For more details on the different level of configurations, you can check official Maven documentation, here, in particular the example "Configuring compile to run twice". Further details are also available on the official POM documentation, here, Plugins section.

like image 113
A_Di-Matteo Avatar answered Sep 18 '22 06:09

A_Di-Matteo