给定下面的示例 POM:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.sample</groupId>
<artifactId>sample-project</artifactId>
<version>0.0.2-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>print-hello</id>
<phase>validate</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<echo message="hello there!" />
</target>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.5.0</version>
<executions>
<execution>
<id>exec-echo</id>
<phase>validate</phase>
<configuration>
<executable>cmd</executable>
<arguments>
<argument>/C</argument>
<argument>echo</argument>
<argument>hello-from-exec</argument>
</arguments>
</configuration>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>print-hello-2</id>
<phase>validate</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<echo message="hello there 2!" />
</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
我们实际上是在配置:
maven-antrun-plugin
打印hello there!
消息_
exec-maven-plugin
打印hello-from-exec
消息_
maven-antrun-plugin
打印hello there 2!
消息_
目标执行都附加到同一个阶段,validate
我们希望以相同的定义顺序执行。
但是,在调用时(该-q
选项用于准确且仅具有其输出):
mvn validate -q
我们将有输出:
main:
[echo] hello there!
main:
[echo] hello there 2!
hello-from-exec
也就是说,对于同一阶段,Maven 执行了定义的插件,但是合并了相同插件的所有定义的执行(即使定义为不同的plugin
部分),然后按顺序执行它们以合并定义。
不幸的是,没有机制可以避免这种合并。我们用于配置插件执行行为的唯一选项是:
- 配置
inherited
入口:
true
或者false
,这个插件配置是否应该应用于继承自这个插件的 POM。默认值为true
。
combine.children
和combine.self
到
_
通过向配置元素的子元素添加属性来控制子 POM 如何从父 POM 继承配置。
这些选项都不会帮助我们。在这种情况下,我们需要元素merge
上的一种属性execution
或默认情况下具有不同的行为(即,Maven 应该尊重定义顺序)。
从命令行调用单个执行,如下所示:
mvn antrun:run@print-hello exec:exec@exec-echo antrun:run@print-hello-2 -q
相反,我们将获得所需的输出:
main:
[echo] hello there!
hello-from-exec
main:
[echo] hello there 2!
但在这种情况下:
- 我们不依附于任何阶段
- 我们通过命令行直接调用特定的执行(及其配置)(并且通过仅从 Maven 3.3.1开始提供的新功能)
您可以通过脚本或通过 exec-maven-plugin 调用 maven 本身来实现完全相同的效果,但是 - 同样 - 同样适用:不应用阶段,只应用执行序列。