【问题标题】:How to control a tomcat container during the runtime with maven?如何在运行时使用 maven 控制 tomcat 容器?
【发布时间】:2011-01-15 04:48:36
【问题描述】:

我有一个 Maven 项目,它为另一个 Web 应用程序执行集成测试。此应用程序在 tomcat 容器中部署和启动。

对此的配置在“cargo-maven2-plugin”中完成:

 <plugin>
  <groupId>org.codehaus.cargo</groupId>
  <artifactId>cargo-maven2-plugin</artifactId>
  <configuration>
   <wait>false</wait>
   <configuration>
    <type>standalone</type>
    <properties>
     <cargo.hostname>${itest.hostname}</cargo.hostname>
     <cargo.protocol>${itest.protocol}</cargo.protocol>
     <cargo.servlet.port>${itest.port}</cargo.servlet.port>
     <cargo.servlet.uriencoding>UTF-8</cargo.servlet.uriencoding>
     <cargo.jvmargs>-Xmx1024m</cargo.jvmargs>
    </properties>
   </configuration>
   <container>
    <containerId>tomcat6x</containerId>
    <home>${TEST_TOMCAT_HOME}</home>
   </container>
   <deployer>
    <deployables>
     <deployable>
      <groupId>de.apllicationundertest</groupId>
      <artifactId>apllicationundertest</artifactId>
      <type>war</type>
      <!--
       This will test if the app is ready an throw an exception if
       the integration tests start before deployment is finished
      -->
      <pingURL>${itest.protocol}://${itest.hostname}:${itest.port}/${itest.web.context}/main.html 
      </pingURL>
      <pingTimeout>120000</pingTimeout>
      <!--  Setting our context for the integration tests -->
      <properties>
       <context>${itest.web.context}</context>
      </properties>
     </deployable>
    </deployables>
   </deployer>
  </configuration>
  <executions>
   <execution>
    <id>start-container</id>
    <phase>pre-integration-test</phase>
    <goals>
     <goal>start</goal>
     <goal>deploy</goal>
    </goals>
   </execution>
   <execution>
    <id>stop-container</id>
    <phase>post-integration-test</phase>
    <goals>
     <goal>stop</goal>
    </goals>
   </execution>
  </executions>
 </plugin>

测试中的(web-)应用程序作为依赖项集成在我的集成测试项目的 pom.xml 中,因此我没有绝对或相对的战争路径。

我的问题是在我的程序运行期间我无法控制 tomcat 容器。虽然我的测试场景需要在某些测试之间停止和重新启动容器(以及重新部署被测应用程序),但 f.e.用于检查容器停止后是否仍有一些活动线程,或者缓存中是否仍有我的应用程序的一些元素,...

  1. 我想配置容器在java之外的启动和停止,最好在pom.xml中。那可能吗?
  2. 我可以指定某些单元测试需要重新启动并执行吗?怎么样?

【问题讨论】:

    标签: java tomcat maven-2 integration-testing cargo


    【解决方案1】:

    我认为这是不可能的,因为启动容器是在pre-integration-test 阶段完成的。为什么需要在重新启动的服务器中运行特定的测试?如果可能,请尝试重写您的测试(进行清理)。

    我想在java之外配置容器的启动和停止,最好在pom.xml中。那可能吗?

    如果您几乎不需要这个,您可以尝试将单独测试的配置放在专用的profiles 中。每个配置文件都将包含 cargo 和 surefire/failsafe 插件的配置。

    我可以指定某些单元测试需要重新启动并执行吗?怎么样?

    您可以指定应该运行的测试。看看surefire/failsafe插件配置

    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>2.5</version>
        <configuration>
          <includes>
            <include>Sample.java</include>
          </includes>
        </configuration>
        <executions>
          <execution>
            <id>integration-test</id>
            <phase>integration-test</phase>
            <goals>
              <goal>integration-test</goal>
            </goals>
          </execution>
          <execution>
            <id>verify</id>
            <phase>verify</phase>
            <goals>
              <goal>verify</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
    

    【讨论】:

    • 谢谢cetnar,我想我会采用那个解决方案。但是如何配置每个配置文件一个接一个地使用?我试过这个:“mvn clean integration-test -P profileA -P profileB”和这个“mvn clean integration-test -P profileA,profileB”,但每个只执行一个配置文件。使用“mvn clean integration-test -P profileA profileB”,还有另一个错误:“您必须指定一个有效的生命周期阶段”。
    【解决方案2】:

    我想在java之外配置容器的启动和停止,最好在pom.xml中。这可能吗?

    您可以在 Maven 中做的就是您当前正在做的事情:分别在预集成测试和集成测试后阶段启动和停止货物。

    我可以指定某些单元测试需要重新启动并执行吗?怎么样?

    不,这是不可能的。 Maven 没有这种粒度,也没有为此提供任何挂钩。如果这确实是您需要的,您有两种选择:

    • 像我在this previous answer 中描述的那样从Java 控制容器。
    • 将测试放在单独的 maven 模块中(以便 maven 为每个模块启动和停止您的容器)。

    【讨论】:

      猜你喜欢
      • 2012-02-20
      • 2011-12-09
      • 2020-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多