我建议创建适当的集成测试并使用以下设置在 maven 构建期间进行集成测试。
您可以使用以下部分来下载 tomcat,或者如果您需要使用存储库中的工件。
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<configuration>
    <wait>false</wait>
    <container>
        <containerId>tomcat${tomcat.major}x</containerId>
        <zipUrlInstaller>
            <url>http://archive.apache.org/dist/tomcat/tomcat-${tomcat.major}/v${tomcat.version}/bin/apache-tomcat-${tomcat.version}.tar.gz</url>
            <extractDir>${project.build.directory}/extract/</extractDir>
            <downloadDir>${project.build.directory}/download/</downloadDir>
        </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.tomcat.ajp.port>9008</cargo.tomcat.ajp.port>
        </properties>
    </configuration>
</configuration>
下一部分用于启动并将您的应用程序部署到给定的 tomcat(也与码头一起使用)。
  <executions>
    <execution>
        <id>start-container</id>
        <phase>pre-integration-test</phase>
        <goals>
            <goal>start</goal>
            <goal>deploy</goal>
        </goals>
        <configuration>
            <deployer>
                <deployables>
                    <deployable>
                        <groupId>${project.groupId}</groupId>
                        <artifactId>mod-war</artifactId>
                        <type>war</type>
                        <pingURL>http://localhost:9080/mod-war</pingURL>
                        <pingTimeout>30000</pingTimeout>
                        <properties>
                            <context>mod-war</context>
                        </properties>
                    </deployable>
                </deployables>
            </deployer>
        </configuration>
    </execution>
当然最后通过以下方式停止启动的服务器:
<execution>
    <id>stop-container</id>
    <phase>post-integration-test</phase>
    <goals>
        <goal>stop</goal>
    </goals>
</execution>
最好的办法是将这种配置等放入一个单独的 maven 模块中,该模块可能称为 app-it(用于集成测试)。完整的测试周期可以简单地调用
mvn verify
而在验证生命周期中,集成测试阶段将运行并启动上述配置。但重要的是配置maven-failsafe-plugin以使集成测试本身运行。关于这个的详细描述可以在Maven 单元和集成测试指南中找到
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-failsafe-plugin</artifactId>
  <version>2.12</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>