2012-02-10 69 views
7

我试图测试客户端/服务器应用程序多次,并使用Maven处理构建/测试/部署。要测试应用程序,我需要:运行EXEC-Maven的插件在单相

  1. 运行一个安装脚本(安装服务器),
  2. 揭开序幕的启动命令(启动服务),
  3. 运行测试(maven-神火-插件),
  4. 停止服务,并
  5. 卸载该服务。

步骤1,2,4和5将使用maven-EXEC的插件。第3步将使用maven-surefire插件。

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

有没有人遇到此之前,或不知道如何构建插件的和执行的?

+0

,我认为,你不能优化。 'maven-surefire-plugin' +'maven-surefire-plugin' +'maven-exec-plugin'是你能做到的唯一方法(除非你足够勇敢地编写你自己的maven插件,将它结合起来)。 – 2012-02-12 13:58:37

回答

4

你试图做的事听起来更像是一个集成测试,而不是单元测试。对于这种使用情况下,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> 
+0

这或多或少是我去过的路线。我不确定Maven为什么缺少预测试和后测阶段,但使用集成测试阶段就足够了。 – SlimyTadpole 2012-02-13 15:29:23

10

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

(注:我在地方真正的安装和清理命令的回声命令)

<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> 
+0

这个插件的版本是什么?我无法让插件在版本1.2.1的执行中使用执行 – avanderw 2012-09-21 08:37:15