相关文章推荐
刀枪不入的金针菇  ·  c++ - Qtimer ...·  1 年前    · 
体贴的柿子  ·  安装apt-utils-掘金·  2 年前    · 
朝气蓬勃的伤疤  ·  Oops!!! - 简书·  2 年前    · 
博学的汤圆  ·  get json sub data ...·  2 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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.

    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 JonnyRaa Jan 5, 2015 at 15:43

    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:

  • maven-plugin-3 (generate-resources)
  • maven-plugin-1 (process-resources)
  • maven-plugin-2 (process-resources)
  • 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 :

  • maven.plugin.2
  • maven.plugin.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> 
    
  • 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>
                    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 at 23:57
    

    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:

  • 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.
  • "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.

    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 4, 2021 at 9:39

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.