如何设置同一阶段不同Maven插件的执行顺序
问题描述
- 需求:在
maven-antrun-plugin执行前后分别打印消息 - 第一次尝试:重复声明
echo-maven-plugin,报错提示同一插件只能配置一次 - 第二次尝试:将两个echo执行配置放在同一个
echo-maven-plugin内,无报错但两条消息都在antrun执行前打印
第一次尝试配置代码:
<plugins> <plugin> <groupId>com.soebes.maven.plugins</groupId> <artifactId>echo-maven-plugin</artifactId> <version>0.5.0</version> <executions> <execution> <id>echo-before-call-plugin</id> <phase>compile</phase> <goals> <goal>echo</goal> </goals> <configuration> <echos> <echo>Before run plugin antrun.</echo> </echos> </configuration> </execution> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <executions> <execution> <phase>compile</phase> <configuration> <tasks> ..... </tasks> </configuration> <goals> <goal>run</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>com.soebes.maven.plugins</groupId> <artifactId>echo-maven-plugin</artifactId> <version>0.5.0</version> <executions> <execution> <id>echo-before-call-plugin</id> <phase>compile</phase> <goals> <goal>echo</goal> </goals> <configuration> <echos> <echo>After run plugin antrun.</echo> </echos> </configuration> </execution> </plugin> </plugins>
第二次尝试配置代码:
<plugins> <plugin> <groupId>com.soebes.maven.plugins</groupId> <artifactId>echo-maven-plugin</artifactId> <version>0.5.0</version> <executions> <execution> <id>echo-before-call-plugin</id> <phase>compile</phase> <goals> <goal>echo</goal> </goals> <configuration> <echos> <echo>Before run plugin antrun.</echo> </echos> </configuration> </execution> <execution> <id>echo-after-call-plugin</id> <phase>compile</phase> <goals> <goal>echo</goal> </goals> <configuration> <echos> <echo>After run plugin antrun.</echo> </echos> </configuration> </execution> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <executions> <execution> <phase>compile</phase> <configuration> <tasks> ..... </tasks> </configuration> <goals> <goal>run</goal> </goals> </execution> </executions> </plugin> </plugins>
原因分析
第二次尝试的问题在于:所有执行(两个echo、一个antrun)都绑定到了同一个compile阶段。Maven在执行生命周期阶段时,会按插件在pom中的顺序执行所有绑定到该阶段的插件目标——因为echo-maven-plugin在maven-antrun-plugin之前,所以它的两个execution会先全部执行完毕,才会轮到antrun执行。
解决方案
要实现"before -> antrun -> after"的执行顺序,有三种可靠方式:
方式一:利用生命周期阶段的先后顺序
将"before" echo绑定到compile阶段,antrun也绑定到compile,将"after" echo绑定到compile之后的阶段(比如process-classes),利用Maven生命周期的阶段顺序来控制执行:
<plugins> <plugin> <groupId>com.soebes.maven.plugins</groupId> <artifactId>echo-maven-plugin</artifactId> <version>0.5.0</version> <executions> <!-- 绑定到compile阶段,在antrun之前执行 --> <execution> <id>echo-before-antrun</id> <phase>compile</phase> <goals> <goal>echo</goal> </goals> <configuration> <echos> <echo>Before run plugin antrun.</echo> </echos> </configuration> </execution> <!-- 绑定到compile之后的process-classes阶段 --> <execution> <id>echo-after-antrun</id> <phase>process-classes</phase> <goals> <goal>echo</goal> </goals> <configuration> <echos> <echo>After run plugin antrun.</echo> </echos> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <executions> <execution> <id>antrun-execution</id> <phase>compile</phase> <configuration> <tasks> <!-- 你的ant任务 --> </tasks> </configuration> <goals> <goal>run</goal> </goals> </execution> </executions> </plugin> </plugins>
方式二:同一阶段内指定执行顺序(Maven 3.3.1+)
如果希望所有执行都在同一个阶段内完成,可以给每个execution添加execution-order配置(需Maven 3.3.1及以上版本),明确指定执行顺序:
<plugins> <plugin> <groupId>com.soebes.maven.plugins</groupId> <artifactId>echo-maven-plugin</artifactId> <version>0.5.0</version> <executions> <execution> <id>echo-before-antrun</id> <phase>compile</phase> <!-- 指定此执行先于antrun的execution --> <execution-order>before</execution-order> <goals> <goal>echo</goal> </goals> <configuration> <echos> <echo>Before run plugin antrun.</echo> </echos> </configuration> </execution> <execution> <id>echo-after-antrun</id> <phase>compile</phase> <!-- 指定此执行后于antrun的execution --> <execution-order>after</execution-order> <goals> <goal>echo</goal> </goals> <configuration> <echos> <echo>After run plugin antrun.</echo> </echos> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <executions> <execution> <id>antrun-execution</id> <phase>compile</phase> <configuration> <tasks> <!-- 你的ant任务 --> </tasks> </configuration> <goals> <goal>run</goal> </goals> </execution> </executions> </plugin> </plugins>
方式三:直接在antrun任务中添加echo
如果不需要单独使用echo-maven-plugin,可以直接在maven-antrun-plugin的tasks前后添加echo任务,这是最直接的方式:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <executions> <execution> <id>antrun-with-echo</id> <phase>compile</phase> <configuration> <tasks> <echo>Before run plugin antrun.</echo> <!-- 你的ant任务 --> <echo>After run plugin antrun.</echo> </tasks> </configuration> <goals> <goal>run</goal> </goals> </execution> </executions> </plugin>
内容的提问来源于stack exchange,提问作者Jacek Karwatka
相关产品推荐
相关产品推荐

