Main Tutorials

Maven plugin – execution order in same phase

In Maven, you can use the build lifecycle or phases to control the order of the plugin execution. In some scenario, you have to bound few plugins to same phase, but still want to control the order of the plugin execution.

But, the order is not executed in the same order as they are listed in the POM, see this MNG-2258 and MNG-3719.

Solution

This bug is fixed in Maven 3.0.3, Maven plugin bound to same phase will be executed in the same order as they are listed in the pom.xml

For example, 3 plugins bound to the phase “prepare-package“.


<build>
<plugins>
  <plugin>
	<artifactId>maven-clean-plugin</artifactId>
	<version>2.2</version>
	<executions>
		<execution>
		<id>auto-clean</id>
		<phase>prepare-package</phase>
		//...
  </plugin>

  <plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-resources-plugin</artifactId>
	<version>2.5</version>
	<executions>
		<execution>
		<id>copy-resources</id>
		<phase>prepare-package</phase>
		//...
  </plugin>

  <plugin>
	<groupId>org.primefaces.extensions</groupId>
	<artifactId>resources-optimizer-maven-plugin</artifactId>
	<version>0.5</version>
	<executions>
	        <execution>
		<id>optimize</id>
		<phase>prepare-package</phase>
		//...
  </plugin>

</plugins>
</build>

If you run mvn prepare-package, it will execute in following sequence, just as the order they are listed in pom.xml

  1. maven-clean-plugin
  2. maven-resources-plugin
  3. resources-optimizer-maven-plugin

References

  1. plugin execution ordering no longer POM ordered in 2.0.9
  2. How to perform ordered tasks in Maven build
  3. Wrong execution order of plugins in same phase

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
4 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Alexander Kriegisch
2 years ago

For me this works for plugins declared in the same profile, but not if some plugins are declared within a profile, others outside of profiles. Tested on Maven 3.6.3 and painfully noticed because I had a hard to diagnose problem with changing execution order of plugins within the same phase, depending on which profiles were active. See my inquiry on the Maven Users mailing list.

Mirec Za?ko
8 years ago

seems not to work for plugins in same phase, both plugins in same profile

akshay
9 years ago

how about the profiles in the same phase. Can this declaration order applies to there too..

FibreFoX
8 years ago
Reply to  akshay

You can run “mvn help:effective-pom” to see the final order, when having a profile, you should specify it “mvn help:effective-pom -PmyProfileName”