Changing the order of maven plugin execution

2019-02-05 18:11发布

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.

标签: maven-3
3条回答
ゆ 、 Hurt°
2楼-- · 2019-02-05 18:34

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>
查看更多
我只想做你的唯一
3楼-- · 2019-02-05 18:45

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
查看更多
叼着烟拽天下
4楼-- · 2019-02-05 18:56

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)
查看更多
登录 后发表回答