Changing the order of maven plugin execution

Solution 1:

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)

Solution 2:

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

Solution 3:

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>

Solution 4:

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.