[英]Maven: run plugin twice during a phase, interleaved with another plugin
对于我们的end-2-end测试,我们需要执行以下逻辑流程:
pre-integration-test
) pre-integration-test
) pre-integration-test
) pre-integration-test
) integration-test
)中运行Web应用程序 post-integration-test
) post-integration-test
) 对于运行SQL,使用sql-maven-plugin
,但是此流程不适合常规POM布局:
liquibase-maven-plugin
之前和之后两次pre-integration-test
期间运行 pre-integration-test
,但是它必须在后运行post-integration-test
,这样的Tomcat关闭后的DB模式被丢弃。 据我所知,从Maven文档中可以看出,POM中插件的顺序定义了同一阶段的执行顺序,并且插件在同一个POM中不能被提及两次。
问题 :有没有办法实现这一点,除了编写一个多次调用Maven的shell脚本?
PS发现了一个类似的悬而未决的问题 。
鉴于下面的样本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!
打印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继承配置。
这些选项都不会对我们有所帮助。 在这种情况下,我们需要在execution
元素上使用一种merge
属性,或者默认情况下具有不同的行为(也就是说,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!
但在这种情况下:
您可以通过脚本或通过exec-maven-plugin调用maven本身来实现完全相同,但是 - 再次 - 同样适用:没有应用阶段,只有执行序列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.