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.
My maven project looks similar to this:
- Parent Module
- Module A
- Module 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.
What's the best practice to do this? Use the 开发者_JAVA技巧mojo-exec plugin? How to configure that? Using a "dirty" shell script and executing mvn run for module A?
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). Running means to start a packed (that works) module's Main class!
Try to use the java goal of maven-exec-plugin bound to install phase:
<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.
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. The following is a snippet to configure the cargo plugin to start/stop a tomcat server, but cargo supports also JBoss, Glassfish etc.
<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)
<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.
精彩评论