简体   繁体   English

在集成测试阶段执行Maven模块

[英]Execute a Maven module in integration test phase

I want to start a sibling maven 3 module which acts as an application server in one of my maven modules to run integration tests against the system. 我想启动一个同级Maven 3模块,该模块在我的一个Maven模块中充当应用程序服务器,以对系统运行集成测试。

My maven project looks similar to this: 我的Maven项目看起来与此类似:

  • Parent Module 父模块
    • Module A 模块A
    • Module B 模块B

Now I want to start "Module A" in the pre-integration-test phase of maven and then run all integration tests contained in Module B. I managed to run the integration tests in Module B but did not find a "slick" way to startup Module B in the pre-integration-test phase. 现在,我想在Maven的集成前测试阶段中启动“模块A”,然后运行模块B中包含的所有集成测试。我设法在模块B中运行了集成测试,但是没有找到“光滑”的方式在集成前测试阶段启动模块B。

What's the best practice to do this? 最佳做法是什么? Use the mojo-exec plugin? 使用mojo-exec插件? How to configure that? 如何配置? Using a "dirty" shell script and executing mvn run for module A? 使用“脏” shell脚本并执行模块A的mvn运行?

Edit 编辑

I don't want to start an application server in pre-integration-test phase but want to run a maven module (which itself is started the mojo-exec-plugin). 我不想在集成前测试阶段启动应用程序服务器,而是想运行一个Maven模块(它本身是由mojo-exec-plugin启动的)。 Running means to start a packed (that works) module's Main class! 运行意味着启动一个打包的(有效的)模块的Main类!

Try to use the java goal of maven-exec-plugin bound to install phase: 尝试使用绑定到安装阶段的maven-exec-plugin的java目标:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.2.1</version>
    <executions>
      <execution>
        <id>start-app</id>
        <phase>install</phase>
        <goals>
          <goal>java</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
      <mainClass>com.example.Main</mainClass>
      <arguments>
        <argument>argument1</argument>
        ...
      </arguments>
      <systemProperties>
        <systemProperty>
          <key>myproperty</key>
          <value>myvalue</value>
        </systemProperty>
        ...
      </systemProperties>
    </configuration>
  </plugin>

First you should put your integration tests into a single module let us assume you use Module A than you you should run the integration test phase also on the same module and not into a different module. 首先,您应该将集成测试放在单个模块中,让我们假设您使用模块A,而不是应该在同一模块而不是不同模块上运行集成测试阶段。

You can use cargo-plugin to make a startup for an application server during the pre-integration-test phase run your integration-tests via the maven-failsafe-plugin and shut down you applcation server during the post-integration-test via the cargo plugin. 在集成前测试阶段,您可以使用cargo-plugin为应用程序服务器启动,通过maven-failsafe-plugin运行集成测试,并在集成后测试期间,通过cargo关闭应用程序服务器。插入。 The following is a snippet to configure the cargo plugin to start/stop a tomcat server, but cargo supports also JBoss, Glassfish etc. 以下是用于配置cargo插件以启动/停止tomcat服务器的代码段,但是cargo也支持JBoss,Glassfish等。

   <plugin>
      <groupId>org.codehaus.cargo</groupId>
      <artifactId>cargo-maven2-plugin</artifactId>
      <version>1.0.5</version>
      <configuration>
        <wait>false</wait>
        <container>
          <containerId>tomcat${tomcat.major}x</containerId>
          <zipUrlInstaller>
            <url>http://www.apache.org/dist/tomcat/tomcat-${tomcat.major}/v${tomcat.version}/bin/apache-tomcat-${tomcat.version}.tar.gz</url>
            <installDir>${installDir}</installDir>
          </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>
            <cargo.jvmargs>-DHUDSON_HOME=${project.build.directory}/hudson-storage</cargo.jvmargs>
          </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>org.jvnet.hudson.main</groupId>
                  <artifactId>hudson-war</artifactId>
                  <type>war</type>
                  <pingURL>http://localhost:9080/hudson</pingURL>
                  <pingTimeout>60000</pingTimeout>
                  <properties>
                    <context>hudson</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>

The following snippet gives you an impression of how to use maven-surefire plugin and the compiler plugin: Important in this case to name the integration tests (like XYZ*IT.jav) 以下代码片段给您留下了如何使用maven-surefire插件和编译器插件的印象:在这种情况下,命名集成测试非常重要(例如XYZ * IT.jav)

   <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>2.3.2</version>
      <executions>
        <execution>
          <goals>
            <goal>testCompile</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-failsafe-plugin</artifactId>
      <version>2.6</version>
      <executions>
        <execution>
          <id>integration-test</id>
          <goals>
            <goal>integration-test</goal>
          </goals>
        </execution>
        <execution>
          <id>verify</id>
          <goals>
            <goal>verify</goal>
          </goals>
        </execution>
      </executions>
    </plugin>

You don't need exec plugin etc. or a shell script to run Integration tests in maven. 您不需要exec插件等或shell脚本即可在maven中运行集成测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM