java - 如何在运行时使用 maven 控制 tomcat 容器?

标签 java tomcat maven-2 integration-testing cargo

我有一个 Maven 项目,它为另一个网络应用程序执行集成测试。此应用程序在 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 中,所以我没有绝对或相对的 war 路径。

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

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

最佳答案

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

I want to configure the starting and stopping of the container outside java, preferably in the pom.xml. Is that possible?

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

Can I specify that certain unit tests require a restarting and execute that? How?

您可以指定应该运行什么测试。看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>

关于java - 如何在运行时使用 maven 控制 tomcat 容器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2235786/

相关文章:

maven-2 - 使用 Maven 的外部配置文件

java - 来自 codehouse 的 Maven 动物嗅探器插件找不到 org.jvnet 签名

java - 使用 DatastoreService 的查询字符串

java - 通过 JDBC 使用不同数据库方言的模式

java - 使用 FragmentPagerAdapter 时 Android 崩溃

java - 未找到当前线程 : Spring 3 and Hibernate 4 (Using @PostConstruct annotation) 的 session

java - mchange-commons-java-0.2.11.jar 的 Spring Boot Web 应用程序 FileNotFoundException

java - joda-time 1.6.2 jar 未从 Maven 中央存储库下载

apache - 所有请求如何使用同一端口连接到 Web 服务器?

windows - 可移植 JDK、Tomcat 和 Eclipse (Windows)