部署到集成服务器并使用Maven运行冒烟测试

时间:2011-08-03 10:56:17

标签: deployment maven continuous-integration

我们正在使用Maven构建一个Web应用程序。

现在我们希望我们的CI(连续集成)服务器自动将WAR部署到用作alpha测试服务器的远程Tomcat实例。

我们使用cargo-maven-plugin来做到这一点,并且工作正常。

但是,现在我们还希望在部署后对远程Tomcat进行冒烟测试,以确保(重新)部署干净利落。

我们有测试(使用HtmlUnit),但我们在将它们集成到Maven时遇到了麻烦。到目前为止,我们直接开始货运(使用mvn cargo:redeploy)。这很有效 - 但是,之后我们找不到办法进行烟雾测试。

那么我们如何才能将maven设置为

  • 首次使用货物部署
  • 然后进行烟雾测试

maven构建生命周期似乎没有这个地方。 我们需要定义自定义构建生命周期吗?我们能以某种方式绑定到默认的生命周期阶段吗?哪一个?

1 个答案:

答案 0 :(得分:9)

您必须做的最重要的事情是将您的集成放入一个单独的maven模块中调用它(用于集成测试)。将货物插件放入集成阶段:

<plugin>
    <groupId>org.codehaus.cargo</groupId>
    <artifactId>cargo-maven2-plugin</artifactId>
    <configuration>
      <wait>false</wait>
      <container>
        <containerId>tomcat${tomcat.major}x</containerId>
        <zipUrlInstaller>
          <url>http://archive.apache.org/dist/tomcat/tomcat-${tomcat.major}/v${tomcat.version}/bin/apache-tomcat-${tomcat.version}.tar.gz</url>
          <downloadDir>${project.build.directory}/cargo/download/</downloadDir>
          <extractDir>${installDir}</extractDir>
        </zipUrlInstaller>
        <output>${project.build.directory}/tomcat${tomcat.major}x.log</output>
        <log>${project.build.directory}/cargo.log</log>
      </container>
      <configuration>
        <home>${project.build.directory}/tomcat-${tomcat.version}/container</home>
        <properties>
          <cargo.logging>high</cargo.logging>
          <cargo.servlet.port>9080</cargo.servlet.port>
        </properties>
      </configuration>
    </configuration>

添加执行块:

  <executions>
      <execution>
        <id>start-container</id>
        <phase>pre-integration-test</phase>
        <goals>
          <goal>start</goal>
          <goal>deploy</goal>
        </goals>
        <configuration>
          <deployer>
            <deployables>
              <deployable>
                <groupId>${project.groupId}</groupId>
                <artifactId>mod-war</artifactId>
                <type>war</type>
                <pingURL>http://localhost:9080/mod-war</pingURL>
                <pingTimeout>30000</pingTimeout>
                <properties>
                  <context>mod-war</context>
                </properties>
              </deployable>
            </deployables>
          </deployer>
        </configuration>
      </execution>

和关闭部分:

      <execution>
        <id>stop-container</id>
        <phase>post-integration-test</phase>
        <goals>
          <goal>stop</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

上面的配置描述了一个完整的集成测试周期,下载了一个tomcat解压缩tomcat的发行版部署war文件(dependend项目到tomcat),启动web应用程序并最终停止Tomcat。为了您的目的,您需要更改的是您没有启动阶段,因为您已经有一个正在运行的容器。如何做到这一点在cargo documentation中有所描述。最后,您必须将集成测试放入集成测试模块的src / test / java文件夹中,并且不要忘记配置maven-surefire-plugin,如下所示:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <executions>
      <execution>
        <phase>integration-test</phase>
        <goals>
          <goal>test</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
      <printSummary>true</printSummary>
      <testFailureIgnore>true</testFailureIgnore>
    </configuration>
  </plugin>

您可以查看完整示例here。对此进行了描述here