gpt4 book ai didi

java - Maven:在一个阶段运行插件两次,与另一个插件交错

转载 作者:搜寻专家 更新时间:2023-11-01 03:00:06 25 4
gpt4 key购买 nike

对于我们的端到端测试,我们需要执行以下逻辑流程:

  1. 在数据库中创建和设置 e2e 模式(用户)(pre-integration-test)
  2. 运行 Liquibase 以初始填充模式(pre-integration-test)
  3. 将特定于端到端的测试数据添加到数据库表(pre-integration-test)
  4. 启动Tomcat(pre-integration-test)
  5. 使用 Protractor 在 Tomcat 中运行 Web 应用程序(integration-test)
  6. 关闭 Tomcat(post-integration-test)
  7. 清理数据库:删除模式(post-integration-test)

为了运行 SQL,使用了 sql-maven-plugin,但是这个流程不适合常规的 POM 布局:

  • SQL 插件必须在pre-integration-test期间运行两次,之前之后 liquibase-maven-plugin
  • SQL 插件必须在 pre-integration-test 期间运行 before Tomcat 插件,但是它必须在 期间运行 after post-integration-test,以便在 Tomcat 关闭后删除数据库架构。

据我从 Maven docs 得出的结论,插件在POM中的顺序定义了同一阶段的执行顺序,一个插件不能在同一个POM中被提及两次。

问题:除了编写一个会多次调用 Maven 的 shell 脚本之外,还有什么方法可以实现这一点吗?

附言找到一个类似的 unanswered question .

最佳答案

给定以下示例 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-from-exec 消息的exec-maven-plugin
  • 打印 hello there 2! 消息的 maven-antrun-plugin

目标执行都附加到同一个阶段,validate,我们希望以相同的定义顺序执行。

但是,调用时(-q 选项用于准确且仅具有它们的输出):

mvn validate -q

我们的输出是:

main:
[echo] hello there!

main:
[echo] hello there 2!
hello-from-exec

也就是说,对于同一阶段,Maven 执行定义的插件,但是合并所有为相同插件定义的执行(即使定义为不同的 plugin 部分),然后在合并定义的顺序。

不幸的是,没有任何机制可以避免这种合并。我们配置插件执行行为的唯一选项是:

  • inherited配置入口:

    true or false, whether or not this plugin configuration should apply to POMs which inherit from this one. Default value is true.

  • combine.childrencombine.self

    control how child POMs inherit configuration from parent POMs by adding attributes to the children of the configuration element.

这些选项都对我们没有帮助。在这种情况下,我们需要 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!

但在这种情况下:

  • 我们不依附于任何阶段
  • 我们通过命令行(以及通过 Maven 3.3.1
  • 后才可用的新 feature 直接调用特定执行(及其配置)

您可以通过脚本或通过 exec-maven-plugin 调用 maven 本身来实现完全相同的效果,但是 - 再一次 - 同样适用:没有应用阶段,只有执行顺序。

关于java - Maven:在一个阶段运行插件两次,与另一个插件交错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37434109/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com