Changing the order of maven plugin execution

user1062115 picture user1062115 · Nov 23, 2011 · Viewed 34.2k times · Source

I am new to maven. I would like to change the order of maven plug-in execution.

In my pom.xml, I have maven-assembly-plugin and maven-ant-plugin .I have used maven-assembly-plugin for creating zip file and maven-ant-plugin for copying the zip file from target to some other directory. When I run pom.xml, maven-ant- plugin got triggered and looking for zip file finally I got the error saying zip file not found. Please suggest me the way how to run maven-assembly-plugin first after that maven-ant-plugin needs to run so that it will copy zip file to the corresponding directory.

Answer

Sri Sankaran picture Sri Sankaran · Nov 23, 2011

Since you say you are very new to Maven....Maven builds are executions of an ordered series of phases. These phases are determined by the lifecycle that is appropriate to your project based on its packaging.

Therefore, you control when a plugin's goal is executed by binding it to a particular phase.

Hope that helps.

EDIT: Also, since Maven 3.0.3, for two plugins bound to the same phase, the order of execution is the same as the order in which you define them. For example:

<plugin>
  <artifactId>maven-plugin-1</artifactId>
  <version>1.0</version>
  <executions>
    <execution>
      <phase>process-resources</phase>
      ...
    </execution>
  </executions>
</plugin> 
<plugin>
  <artifactId>maven-plugin-2</artifactId>
  <version>1.0</version>
  <executions>
    <execution>
      <phase>process-resources</phase>
      ...
    </execution>
  </executions>
</plugin> 
<plugin>
  <artifactId>maven-plugin-3</artifactId>
  <version>1.0</version>
  <executions>
    <execution>
      <phase>generate-resources</phase>
      ...
    </execution>
  </executions>
</plugin>

In the above instance, the execution order would be:

  1. maven-plugin-3 (generate-resources)
  2. maven-plugin-1 (process-resources)
  3. maven-plugin-2 (process-resources)