【问题标题】:Running exec-maven-plugin multiple times in a single phase在一个阶段多次运行 exec-maven-plugin
【发布时间】:2012-03-03 03:35:00
【问题描述】:

我正在尝试测试客户端/服务器应用程序,并使用 Maven 来处理构建/测试/部署。要测试应用程序,我需要:

  1. 运行安装程序脚本(以安装服务器),
  2. 启动一个启动命令(以启动服务),
  3. 运行测试(maven-surefire-plugin),
  4. 停止服务,然后
  5. 卸载服务。

步骤 1、2、4 和 5 将使用 maven-exec-plugin。第 3 步将使用 maven-surefire-plugin。

问题是所有 5 个步骤都将发生在“测试”阶段。 Maven 允许插件以特定顺序执行。 exec-plugin 可以通过使用多个条目多次运行。问题是我需要在 4 次 exec-plugin 执行过程中使用 surefire-plugin。

以前有没有人遇到过这种情况,或者知道如何构建插件和执行?

【问题讨论】:

  • 我认为,你无法优化它。 maven-surefire-plugin + maven-surefire-plugin + maven-exec-plugin 是您可以做到的唯一方法(除非您有足够的勇气编写自己的 maven 插件来为您组合)。

标签: maven maven-2 maven-plugin


【解决方案1】:

这就是我配置 exec 和 failsafe 插件的方式。我正在使用故障保护,而不是重新配置surefire,因为surefire仍在运行其他测试以处于测试阶段。这将在预集成测试阶段运行第 1 步和第 2 步(为同一阶段列出的多个执行将按照给定的顺序执行),在集成测试阶段运行测试,然后使用第 3 步和第 4 步进行清理集成后测试阶段。

(注意:我用 echo 命令代替了真正的安装和清理命令)

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>integration-test</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <forkMode>always</forkMode>
    </configuration>
</plugin>

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>step1</id>
            <goals>
                <goal>exec</goal>
            </goals>
            <phase>pre-integration-test</phase>
            <configuration>
                <executable>echo</executable>
                <arguments>
                    <argument>foo</argument>
                </arguments>
            </configuration>
        </execution>
        <execution>
            <id>step2</id>
            <goals>
                <goal>exec</goal>
            </goals>
            <phase>pre-integration-test</phase>
            <configuration>
                <executable>echo</executable>
                <arguments>
                    <argument>bar</argument>
                </arguments>
            </configuration>
        </execution>
        <execution>
            <id>step3</id>
            <goals>
                <goal>exec</goal>
            </goals>
            <phase>post-integration-test</phase>
            <configuration>
                <executable>echo</executable>
                <arguments>
                    <argument>baz</argument>
                </arguments>
            </configuration>
        </execution>
        <execution>
            <id>step4</id>
            <goals>
                <goal>exec</goal>
            </goals>
            <phase>post-integration-test</phase>
            <configuration>
                <executable>echo</executable>
                <arguments>
                    <argument>woot</argument>
                </arguments>
            </configuration>
        </execution>  
    </executions>
</plugin>

【讨论】:

  • 这是什么版本的插件?我无法使用 1.2.1 版执行中的 执行插件
【解决方案2】:

您正在尝试做的事情听起来更像是集成测试而不是单元测试。对于这个用例,default maven lifecycle 具有与集成测试相关的三个阶段:

  • pre-integration-test:在执行集成测试之前执行所需的操作。这可能涉及诸如设置所需环境之类的事情。
  • integration-test:如有必要,处理包并将其部署到可以运行集成测试的环境中。
  • post-integration-test:执行集成测试后执行所需的操作。这可能包括清理环境。

surefire 插件通常在test 阶段执行,但可以重新配置为在另一个阶段执行。然后可以将步骤 1 + 2 配置为在 pre-integration-test 中执行,而步骤 4 + 5 必须在 post-integration-test 中执行。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <!-- Skip execution in test phase and execute in integration-test phase instead -->
    <configuration>
        <skip>true</skip>
    </configuration>
    <executions>
        <execution>
            <id>surefire-it</id>
            <phase>integration-test</phase>
            <goals>
                <goal>test</goal>
            </goals>
            <configuration>
                <skip>false</skip>
            </configuration>
        </execution>
    </executions>
</plugin>

【讨论】:

  • 这或多或少是我走的路线。我不确定为什么 Maven 缺少测试前和测试后阶段,但是使用集成测试阶段就足够了。
猜你喜欢
  • 2018-07-09
  • 1970-01-01
  • 2011-07-03
  • 2012-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-16
  • 2023-01-24
相关资源
最近更新 更多