给出下面的 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>
我们实际上正在配置:
- The
maven-antrun-plugin
打印hello there!
message
- The
exec-maven-plugin
打印hello-from-exec
message
- The
maven-antrun-plugin
打印hello there 2!
message
目标执行都属于同一阶段,validate
,并且我们期望以相同的定义顺序执行。
然而,当调用(-q
选项用于精确且仅具有它们的输出):
mvn validate -q
我们将得到以下输出:
main:
[echo] hello there!
main:
[echo] hello there 2!
hello-from-exec
也就是说,对于同一阶段,Maven 执行定义的插件,但是合并相同插件的所有定义的执行(即使定义为不同的插件)plugin
节),然后按照合并定义的顺序执行它们。
不幸的是,没有机制可以避免这种情况merging。我们用于配置插件执行行为的唯一选项是:
- The inherited https://maven.apache.org/pom.html#Plugins configuration entry:
true
or false
,此插件配置是否应应用于从该插件继承的 POM。默认值为true
.
- The
combine.children
and combine.self
to
通过向配置元素的子元素添加属性来控制子 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!
但在这种情况下:
- 我们不执着于任何相
- 我们通过命令行(以及通过新的feature https://issues.apache.org/jira/browse/MNG-5768仅自 Maven 起可用3.3.1
您可以通过脚本或通过 exec-maven-plugin 调用 maven 本身来实现完全相同的效果,但是同样适用:不应用阶段,仅执行顺序。