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

如何在终止Maven/Quarkus时自动停止Angular前端服务

解决方案:终止Maven/Quarkus时自动停止Angular前端

你的问题核心是:ctrl+c中断Maven进程时,Maven的post-integration-test阶段不会触发,导致原本配置的前端进程终止逻辑无法执行。以下是两种可靠的解决方法:

方法1:利用exec-maven-plugin的Shutdown Hook(推荐)

通过配置exec-maven-plugin的shutdownHook参数,让Maven在进程退出(包括中断信号触发的退出)时自动执行前端进程终止命令。

完整POM配置

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>3.1.0</version>
    <executions>
        <!-- 启动前端服务并保存PID -->
        <execution>
            <id>start-frontend</id>
            <phase>validate</phase>
            <goals>
                <goal>exec</goal>
            </goals>
            <configuration>
                <executable>npm</executable>
                <arguments>
                    <argument>run</argument>
                    <argument>start</argument>
                </arguments>
                <workingDirectory>${project.basedir}/frontend</workingDirectory>
                <fork>true</fork>
                <pidFile>${project.build.directory}/frontend.pid</pidFile>
            </configuration>
        </execution>
        <!-- 正常构建流程结束时终止前端 -->
        <execution>
            <id>stop-frontend-normal</id>
            <phase>post-integration-test</phase>
            <goals>
                <goal>exec</goal>
            </goals>
            <configuration>
                <executable>bash</executable>
                <arguments>
                    <argument>-c</argument>
                    <argument>if [ -f ${project.build.directory}/frontend.pid ]; then kill $(cat ${project.build.directory}/frontend.pid); rm ${project.build.directory}/frontend.pid; fi</argument>
                </arguments>
            </configuration>
        </execution>
        <!-- 中断时触发的Shutdown Hook -->
        <execution>
            <id>stop-frontend-shutdown</id>
            <goals>
                <goal>exec</goal>
            </goals>
            <configuration>
                <executable>bash</executable>
                <arguments>
                    <argument>-c</argument>
                    <argument>if [ -f ${project.build.directory}/frontend.pid ]; then kill $(cat ${project.build.directory}/frontend.pid); rm ${project.build.directory}/frontend.pid; fi</argument>
                </arguments>
                <shutdownHook>true</shutdownHook>
            </configuration>
        </execution>
    </executions>
</plugin>

Windows环境适配

如果是Windows系统,将终止命令替换为CMD命令:

<executable>cmd</executable>
<arguments>
    <argument>/c</argument>
    <argument>if exist ${project.build.directory}/frontend.pid (for /f %%i in (${project.build.directory}/frontend.pid) do taskkill /F /PID %%i) & del ${project.build.directory}/frontend.pid</argument>
</arguments>

方法2:确保前端进程为Maven直接子进程(简化版)

如果你的Angular服务不会再fork额外子进程,可以尝试启用exec-maven-plugin的killAfterExit参数,让Maven退出时自动杀死fork的子进程:

<execution>
    <id>start-frontend</id>
    <phase>validate</phase>
    <goals>
        <goal>exec</goal>
    </goals>
    <configuration>
        <executable>npm</executable>
        <arguments>
            <argument>run</argument>
            <argument>start</argument>
        </arguments>
        <workingDirectory>${project.basedir}/frontend</workingDirectory>
        <fork>true</fork>
        <killAfterExit>true</killAfterExit>
    </configuration>
</execution>

注意事项

  • 该方法仅能杀死Maven直接fork的npm进程,若npm run start又启动了独立的Angular子进程(大部分情况会如此),子进程会成为孤儿进程继续运行,因此方法1更可靠。
  • 若要确保保存的是Angular进程的PID而非npm进程,可以自定义启动脚本(如start-frontend.sh),直接启动ng serve并保存其PID,再通过Maven执行该脚本。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 03:33:16