You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Maven多模块项目中启动嵌入式Tomcat Jar并执行集成测试的方案

现代解决方案:Spring Boot Jar 集成测试启动方案

核心方案:使用spring-boot-maven-plugin的start/stop目标

直接用Spring Boot官方插件替代Cargo,就能实现「启动Jar服务→执行集成测试→停止服务」的完整流程。你遇到的FileNotFoundException是因为参数配置格式错误(把JVM参数写成了文件路径格式),调整配置即可解决。

1. 服务器模块(server/pom.xml)配置

确保Spring Boot插件正确生成可执行Jar:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <!-- 绑定package阶段生成可执行Jar -->
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

2. 集成测试模块(integration/pom.xml)配置

在集成模块中绑定插件的启动/停止目标到测试阶段:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <!-- 测试前启动服务器 -->
                <execution>
                    <id>start-server</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>start</goal>
                    </goals>
                    <configuration>
                        <!-- 指定Spring Boot启动类 -->
                        <mainClass>com.yourpackage.YourSpringBootApplication</mainClass>
                        <!-- 指定服务器模块编译后的class目录,也可以直接指定Jar路径 -->
                        <classesDirectory>${project.parent.basedir}/server/target/classes</classesDirectory>
                        <!-- 正确配置JVM参数 -->
                        <jvmArguments>--spring.application.admin.enabled=true</jvmArguments>
                        <!-- 可选:指定端口避免冲突 -->
                        <arguments>--server.port=8081</arguments>
                    </configuration>
                </execution>
                <!-- 测试后停止服务器 -->
                <execution>
                    <id>stop-server</id>
                    <phase>post-integration-test</phase>
                    <goals>
                        <goal>stop</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <!-- 用Failsafe插件运行集成测试,避免测试失败时提前终止流程 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

关键注意事项

  • 你之前的错误核心是参数配置格式错误:必须把--spring.application.admin.enabled=true放在<jvmArguments>或<arguments>节点中,不能让插件将其解析为文件路径。
  • 集成测试必须使用maven-failsafe-plugin,它专门处理集成测试场景,即便测试失败也会保证服务器正常停止。
  • 若不想依赖服务器模块的target/classes目录,可直接指定打包后的Jar路径:<jar>${project.parent.basedir}/server/target/server-1.0.0-SNAPSHOT.jar</jar>,替换为实际Jar文件名即可。

替代方案:使用@SpringBootTest(轻量集成测试场景)

如果不需要单独启动外部进程,可直接在测试上下文启动Spring Boot应用,适合无需完全模拟生产环境的场景:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@TestPropertySource(properties = {"server.port=8081", "spring.application.admin.enabled=true"})
public class IntegrationTests {
    // 测试逻辑,直接调用localhost:8081的接口即可
}

内容的提问来源于stack exchange,提问作者Shane Gannon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 10:00:20