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

多模块Maven项目如何一键启动Spring Boot服务与JavaFX应用及解决测试依赖问题

一键启动服务与前端应用方案

有两种常用实现方式:

方案1:启动脚本(最简便)

根据操作系统写对应执行脚本,直接调用两个模块的启动命令即可:

  • Linux/Mac 下创建start.sh:
# 后台启动rest服务
cd rest && mvn spring-boot:run &
# 等待rest服务启动完成,可根据实际启动速度调整时长
sleep 15
# 启动JavaFX UI
cd ../ui && mvn javafx:run

赋予执行权限后直接运行./start.sh即可。

  • Windows 下创建start.bat:
@echo off
start /B cmd /c "cd rest && mvn spring-boot:run"
timeout /t 15 /nobreak
cd ui && mvn javafx:run

双击或者在cmd执行start.bat即可启动全量应用。

方案2:Maven Profile 配置

如果希望纯用Maven实现,可在父POM中新增自定义run-allProfile,绑定两个模块的启动目标:

<profiles>
    <profile>
        <id>run-all</id>
        <build>
            <plugins>
                <!-- 启动rest服务 -->
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <workingDirectory>${project.basedir}/rest</workingDirectory>
                        <!-- 异步启动,避免阻塞后续UI启动 -->
                        <fork>true</fork>
                        <wait>10000</wait>
                    </configuration>
                    <executions>
                        <execution>
                            <id>run-rest</id>
                            <phase>verify</phase>
                            <goals>
                                <goal>run</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <!-- 启动UI应用 -->
                <plugin>
                    <groupId>org.openjfx</groupId>
                    <artifactId>javafx-maven-plugin</artifactId>
                    <configuration>
                        <workingDirectory>${project.basedir}/ui</workingDirectory>
                    </configuration>
                    <executions>
                        <execution>
                            <id>run-ui</id>
                            <phase>verify</phase>
                            <goals>
                                <goal>run</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

后续执行mvn verify -Prun-all即可一键启动全应用。


测试阶段自动启动Web服务方案

依赖Web服务的测试属于集成测试范畴,可借助Maven默认的集成测试生命周期自动管理服务启停:

  1. 首先区分单元测试与集成测试,单元测试用maven-surefire-plugin在test阶段执行,不需要依赖外部服务;集成测试用maven-failsafe-plugin在integration-test阶段执行,依赖rest服务。
  2. 在rest模块的POM中绑定Spring Boot服务的启停到集成测试前后阶段:
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <!-- 集成测试前启动服务 -->
                <execution>
                    <id>pre-integration-test-start-rest</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>start</goal>
                    </goals>
                    <configuration>
                        <!-- 最长等待服务启动10秒,超时则构建失败 -->
                        <wait>10000</wait>
                        <maxAttempts>3</maxAttempts>
                    </configuration>
                </execution>
                <!-- 集成测试后停止服务 -->
                <execution>
                    <id>post-integration-test-stop-rest</id>
                    <phase>post-integration-test</phase>
                    <goals>
                        <goal>stop</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <!-- 集成测试插件 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>3.2.5</version>
            <executions>
                <execution>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
  1. 父POM中调整模块构建顺序,确保rest模块先于其他需要调用它的测试模块构建。

配置完成后,执行mvn clean install时会自动按顺序执行:启动rest服务→运行所有集成测试→停止rest服务,无需手动干预就能保证测试依赖的Web服务处于可用状态。


内容的提问来源于stack exchange,提问作者Alexander Brandhaug

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 18:45:03