6

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

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

对于运行 SQL,sql-maven-plugin使用的是,但是此流程不适合常规 POM 布局:

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

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

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

PS发现了一个类似的未回答的问题

4

1 回答 1

3

给定下面的示例 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.childrencombine.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 本身来实现完全相同的效果,但是 - 同样 - 同样适用:不应用阶段,只应用执行序列。

于 2016-05-26T10:03:28.080 回答