Maven:在一个阶段运行插件两次,与另一个插件交错 [英] Maven: run plugin twice during a phase, interleaved with another plugin
问题描述
对于我们的end-2-end测试,我们需要执行以下逻辑流程:
For our end-2-end test we need to execute the following logical flow:
- 创建并设置e2e架构(用户) )在数据库中(
pre-integration-test
) - 运行Liquibase以初始填充模式(
pre -integration-test
) - 将特定于e2e的测试数据添加到数据库表中(
pre-integration-test
) - 启动Tomcat(
预集成测试
) - 运行网络使用Protractor在Tomcat中应用(
integration-test
) - 关闭Tomcat(
post-integration-test
) - 清理数据库:删除架构(
post-integration-test
)
- Create and set up e2e schema (user) in the database (
pre-integration-test
) - Run Liquibase to initially populate the schema (
pre-integration-test
) - Add e2e-specific test data to the DB tables (
pre-integration-test
) - Start Tomcat (
pre-integration-test
) - Run the web application in Tomcat (
integration-test
) using Protractor - Shut down Tomcat (
post-integration-test
) - Clean up the DB: drop the schema (
post-integration-test
)
对于运行SQL,使用了 sql-maven-plugin
,但此流程没有适合常规POM布局:
For running SQL the sql-maven-plugin
is used, however this flow doesn't fit the regular POM layout:
- SQL插件必须在
预集成测试$ c期间运行$ c>两次,之前 <$ strong后 c> liquibase-maven-plugin
- SQL插件必须在
之前运行之前 Tomcat插件integration-test
,但它必须在post-integration-test
期间之后运行,以便DB模式为在Tomcat关闭后删除。
- The SQL plugin has to run during
pre-integration-test
twice, before and after theliquibase-maven-plugin
- The SQL plugin has to run before Tomcat plugin during
pre-integration-test
, however it has to run after duringpost-integration-test
, so that the DB schema is dropped after Tomcat has shut down.
据我所知, Maven docs ,POM中插件的顺序定义了同一阶段的执行顺序,以及插件不能在同一个POM中提及两次。
As far as I could conclude from Maven docs, the order of plugins in the POM defines the order of execution during the same phase, and a plugin cannot be mentioned twice in the same POM.
问题:除了编写一个可以调用的shell脚本之外,还有什么方法可以实现这一点Maven多次?
Question: Is there any way to achieve this, apart from writing a shell script that would invoke Maven multiple times?
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
打印你好!
消息 -
exec-maven-plugin
打印hello-from-exec
消息 -
maven-antrun-plugin
打印你好那里2!
消息
- The
maven-antrun-plugin
to print thehello there!
message - The
exec-maven-plugin
to print thehello-from-exec
message - The
maven-antrun-plugin
to print thehello there 2!
message
目标执行都附属于同一阶段,验证
,我们期待以相同的定义顺序执行。
Goal executions are all attached to the same phase, validate
, and we would expect to be executed in the same defined order.
然而,在调用时(使用 -q
选项)确切地,只有他们的输出):
However, when invoking (the -q
option is used to have exactly and only their output):
mvn validate -q
我们有输出:
main:
[echo] hello there!
main:
[echo] hello there 2!
hello-from-exec
也就是说,对于同一阶段,Maven执行了定义的插件,但是为相同的插件合并所有已定义的执行(即使定义为不同的插件
部分),然后按顺序执行它们以合并定义。
That is, for the same phase, Maven executed the defined plugins, however merging all of the defined executions for the same plugins (even if defined as different plugin
sections) and then execute them in the order to merged definitions.
不幸的是,没有机制可以避免这种合并。我们配置插件执行行为的唯一选择是:
Unfortunately, there is no mechanism to avoid this merging. The only options we have for configuring plugins execution behaviors are:
-
继承
配置条目:
- The
inherited
configuration entry:
true
或false
,无论此插件配置是否应该应用于从此继承的POM。默认值为true
。
true
orfalse
, whether or not this plugin configuration should apply to POMs which inherit from this one. Default value istrue
.
组合。子项
和 combine.self
到combine.children
and combine.self
to
控制子POM继承配置的方式通过向配置元素的子元素添加属性,从父POM开始。
control how child POMs inherit configuration from parent POMs by adding attributes to the children of the configuration element.
这些选项都不会对我们有所帮助。在这种情况下,我们需要执行
元素上的一种 merge
属性,或者默认情况下具有不同的行为(是的,Maven应该尊重定义顺序。)
None of these options would help us. In this case we would need a kind of merge
attribute on the execution
element or have a different behavior by default (that is, Maven should respect the definition order).
从命令行调用单次执行如下:
Invoking the single executions from command line as below:
mvn antrun:run@print-hello exec:exec@exec-echo antrun:run@print-hello-2 -q
我们会改为获得所需的输出:
We would instead have the desired output:
main:
[echo] hello there!
hello-from-exec
main:
[echo] hello there 2!
但在这种情况下:
- 我们没有附加到任何阶段
- 我们通过命令行直接调用特定的执行(及其配置)(并通过新的功能仅在Maven 3.3.1
- We are not attached to any phase
- We are invoking directly specific executions (and their configurations) via command line (and via a new feature only available since Maven 3.3.1
您可以通过脚本或通过exec-maven-plugin调用maven本身来实现完全相同,但是 - 再次 - 同样适用:没有应用阶段,只有执行序列。
You can achieve exactly the same via scripting or via exec-maven-plugin invoking maven itself, but - again - the same would apply: no phase applied, only sequence of executions.
这篇关于Maven:在一个阶段运行插件两次,与另一个插件交错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!