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

Maven多模块项目执行install时exec插件运行后构建终止,无BUILD SUCCESS提示

问题根因分析
  • 最核心的原因是exec:java目标默认在Maven主进程内运行代码,若你的com.example.MainClass中显式调用了System.exit()方法,会直接终止整个Maven进程,导致后续install阶段的步骤无法执行,也不会输出构建结果提示。
  • 父子POM的exec插件版本不兼容:父POM声明的是3.0.0版本,子POM单独指定了1.1.1老旧版本,两个版本对async、keepAlive参数的处理逻辑存在差异,导致配置不生效。
  • keepAlive参数仅在异步启动守护线程场景下需要,普通同步执行main方法时配置该参数反而可能导致进程阻塞异常。
解决方案

第一步:排查MainClass代码

删除main方法中所有System.exit()调用,改为正常return或抛出业务异常即可,避免主动退出JVM进程。
如果业务逻辑必须调用System.exit(),则将插件的执行目标从java改为exec,exec目标会启动独立的JVM进程运行代码,不会影响Maven主进程。

第二步:统一插件版本

删除子POM中exec-maven-plugin的<version>标签,直接继承父POM声明的3.0.0版本,避免版本冲突。

第三步:修正插件配置

方案1:使用java目标(同进程运行,性能更高)

适用于main方法无System.exit()调用的场景:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>java</goal>
            </goals>
            <configuration>
                <mainClass>com.example.MainClass</mainClass>
                <!-- 不需要异步可设为false,等main执行完成再走后续构建步骤 -->
                <async>false</async>
                <keepAlive>false</keepAlive>
            </configuration>
        </execution>
    </executions>
</plugin>

方案2:使用exec目标(独立进程运行,兼容性更好)

适用于main方法必须调用System.exit()的场景:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>exec</goal>
            </goals>
            <configuration>
                <executable>java</executable>
                <arguments>
                    <argument>-classpath</argument>
                    <classpath/>
                    <argument>com.example.MainClass</argument>
                    <!-- 按需添加main方法的入参 -->
                </arguments>
                <async>false</async>
            </configuration>
        </execution>
    </executions>
</plugin>

修改配置后重新执行mvn install,即可在main方法执行完成后继续走完剩余构建流程,正常输出BUILD SUCCESS提示。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 06:06:08