如何在Maven 3中运行嵌入式Tomcat 9以进行集成测试?
我正在尝试在Maven 3中运行嵌入式Tomcat 9,以进行集成测试.其他SO答案将我带到了cargo-maven2-plugin
.
I am trying to run embedded Tomcat 9 inside Maven 3 for integration testing purposes. I was led to cargo-maven2-plugin
by other SO answers.
因此,尝试按照此处的说明进行操作:
So, attempting to follow the instructions found here:
https://codehaus-cargo.github.io /cargo/Static+deployment+of+WAR.html
我在一个简单的POM中有这个片段:
I have this fragment in a simple POM:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.7.6</version>
<configuration>
<container>
<containerId>tomcat9x</containerId>
<type>embedded</type>
</container>
<deployables>
<deployable>
<type>war</type>
<properties>
<file>path/to/myapp.war</file>
</properties>
</deployable>
</deployables>
</configuration>
</plugin>
</plugins>
</build>
我尝试使用mvn org.codehaus.cargo:cargo-maven2-plugin:run
失败,并显示以下错误:
It fails with the error:
[INFO] [en2.ContainerRunMojo]已解决的容器工件 容器的org.codehaus.cargo:cargo-core-container-tomcat:jar:1.7.6 tomcat9x [警告]定义的可部署具有相同的groupId和 artifactId作为项目的主要工件,但类型不同. 您已经定义了[war]类型,因为项目的包装是[pom]. 这可能是一个错误,因此该插件将尝试 在项目的依赖项中找到可部署的项目.
[INFO] [en2.ContainerRunMojo] Resolved container artifact org.codehaus.cargo:cargo-core-container-tomcat:jar:1.7.6 for container tomcat9x [WARNING] The defined deployable has the same groupId and artifactId as your project's main artifact but the type is different. You've defined a [war] type wher eas the project's packaging is [pom]. This is possibly an error and as a consequence the plugin will try to find this deployable in the project's dependencies.
我该如何进行这项工作?我只想从Maven内部的嵌入式tomcat9中启动给定的WAR.
How can I make this work? I just want to launch the given WAR in an embedded tomcat9, from within Maven.
尝试了许多排列之后,这终于对我有用:
After trying many permutations, this finally worked for me:
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.7.9</version>
<configuration>
<container>
<systemProperties>
<myvar1>${myEnvVar}</myvar1>
<myvar2>... stuff ...</myvar2>
</systemProperties>
<containerId>tomcat9x</containerId>
<zipUrlInstaller>
<url>https://repo.maven.apache.org/maven2/org/apache/tomcat/tomcat/9.0.29/tomcat-9.0.29.zip</url>
</zipUrlInstaller>
</container>
<deployables>
<deployable>
<groupId>org.codehaus.cargo</groupId>
<artifactId>simple-war</artifactId>
<type>war</type>
<location>path/to/myapp.war</location>
<properties>
<context>myapp</context>
</properties>
</deployable>
</deployables>
<executions>
<execution>
<id>start-server</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop-server</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</configuration>
</plugin>
使用failsafe
插件自动在start
和stop
之间运行集成测试:
Use the failsafe
plugin to automatically run the Integration Tests between the start
and stop
:
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.21.0</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
</configuration>
</execution>
</executions>
</plugin>