59

I am new to maven, I would like to change the order of the maven plugins execution.

In my pom.xml, I have maven-assembly-plugin and maven-ant-plugin.

  • maven-assembly-plugin for creating a zip file.
  • 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 to me the way how to run maven-assembly-plugin first before maven-ant-plugin so that it will find and copy the zip file to the corresponding directory.

starball
  • 20,030
  • 7
  • 43
  • 238
user1062115
  • 591
  • 1
  • 4
  • 4
  • Its worth noting that if you have a step that reads a property from a file you can't use that property in later stages in the pom ... see [this post](http://stackoverflow.com/questions/8541332/maven-read-version-number-from-property-file/27782825#27782825) – JonnyRaa Jan 05 '15 at 15:43

4 Answers4

83

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)
Jin Kwon
  • 20,295
  • 14
  • 115
  • 184
Sri Sankaran
  • 8,120
  • 4
  • 38
  • 47
29

Plugins in the same phase are executed in the declared order.

In the case of pom hierachy, you have to re-declare the plugins from the parent pom (just its groupId and its artifactId) into the child pom to specify the execution order :

Parent pom.xml

<plugins>
    <plugin>
        <groupId>groupid.maven.1</groupId>
        <artifactId>maven-plugin-1</artifactId>
        <version>1.0</version>
        <executions>
            <execution>
                <phase>package</phase>
            </execution>
        </executions>
    </plugin>
</plugins>

Child pom.xml

<plugins>
    <plugin>
        <groupId>groupid.maven.2</groupId>
        <artifactId>maven-plugin-2</artifactId>
        <version>1.0</version>
        <executions>
            <execution>
                <phase>package</phase>
            </execution>
        </executions>
    </plugin>
    <plugin>
        <groupId>groupid.maven.1</groupId>
        <artifactId>maven-plugin-1</artifactId>
    </plugin>
</plugins>

Then the execution is :

  1. maven.plugin.2
  2. maven.plugin.1
Baptiste
  • 449
  • 4
  • 7
23

In Maven 3.0.3 and later, there are two rules

  1. 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.

<plugin>
  <artifactId>maven-plugin-2</artifactId>
  <version>1.0</version>
  <executions>
    <execution>
      <phase>compile</phase>
      ...
    </execution>
  </executions>
</plugin>
<plugin>
  <artifactId>maven-plugin-1</artifactId>
  <version>1.0</version>
  <executions>
    <execution>
      <phase>process-resources</phase>
      ...
    </execution>
  </executions>
</plugin> 
  1. If multiple executions have the same phase, then the first one to be executed will be the built-in one (e.g. maven-compiler-plugin) whose id is default-something, then the other executions will take place in the order they appear in your pom file.

For example, if you have this somewhere in your pom

        <plugin>
            <artifactId>maven-plugin-1</artifactId>
            <version>1.2.3</version>
            <executions>
                <execution>
                    <id>my-compile</id>
                    <phase>compile</phase>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <artifactId>maven-plugin-2</artifactId>
            <version>4.5.6</version>
            <executions>
                <execution>
                    <id>my-compile-2</id>
                    <phase>compile</phase>
                </execution>
            </executions>
        </plugin>

and this anywhere in your effective pom

  <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <executions>
      <execution>
        <id>**default-compile**</id>
        <phase>compile</phase>
        <goals>
          <goal>compile</goal>
        </goals>
      </execution>
      ...
    </executions>
  </plugin>

then maven-compiler-plugin will execute maven-compiler-plugin followed by maven-plugin-1, and maven-plugin-2.

If you want maven-compiler-plugin:compile goal to execute after maven-plugin-1 then you could do this

<plugin>
    <artifactId>maven-plugin-1</artifactId>
    <version>1.2.3</version>
    <executions>
        <execution>
            <id>my-compile</id>
            <phase>compile</phase>
        </execution>
    </executions>
</plugin>
<plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <executions>
        <execution>
            <id>something-other-than-**default-compile**</id>
            <phase>compile</phase>
        </execution>
        <execution>
            <id>**default-compile**</id>
            <phase>none</phase>
            <goals>
                <goal>compile</goal>
            </goals>
        </execution>
    </executions>
</plugin>
mikeyreilly
  • 6,523
  • 1
  • 26
  • 21
  • 1
    ` compile ` isn't required when you specify `none` – Ilya Serbis Feb 20 '21 at 21:08
  • Ohhh...built-in before others...that's huge. Was confused why `maven-antrun-plugin` was running before `rpm-maven-plugin`, despite being the same phase. _Thank you!_ – Glenn Feb 20 '23 at 23:57
9

This might be an old questions and the answers provided are correct, just adding an extra point regarding the case for inheritance because I found myself in this situation and could not figure it out.

There seem to be 2 rules for plugin order execution when no inheritance is provided:

  1. The phases to which the plugins are bound to are executed in the order provided in the documentation: see "Default Lifecycle" on Introduction to the Build Lifecycle Plugin executions are ordered according to their phases.
  2. "In Maven 2.0.5 and above, multiple goals bound to a phase are executed in the same order as they are declared in the POM, however multiple instances of the same plugin are not supported. Multiple instances of the same plugin are grouped to execute together and ordered in Maven 2.0.11 and above" - source

IN THE CASE OF INHERITANCE: "Parent POM bindings execute AFTER child bindings.". So if your plugins are not executed in the order you expect them to (by following the previous two points mentioned in this answer), you should maybe look if they are defined in the parent POM. - Incorrect execution order of plugins in the same phase.

I could not find this being clarely documented anywhere, however there is an open ticket to document the algorithm calculating the order of plugin executions.

Dragos Geornoiu
  • 527
  • 2
  • 8
  • 17
  • 1
    Thanks, for the explanation! I think the same applies to profiles. The order in a profile is ignored if the plugins were defined already in the main build part - at least in my case. It's a bit annoying if you have a plugin for multiple tasks. But maybe its a sign of an overloaded build phase ;-) – Nico Kutscherauer Feb 04 '21 at 09:39