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

如何从多模块Maven项目正确构建可执行Jar包?

多模块Maven项目打包可执行Jar问题解决

问题核心原因

  1. 插件配置位置错误:Spring Boot插件配置在Parent.pom中,但Parent模块本身无业务代码,打包时不会自动包含子模块的类文件。
  2. 插件冲突:同时使用spring-boot-maven-plugin和maven-assembly-plugin,两者打包机制不同,导致Jar结构混乱,无法识别主类和业务类。
  3. Assembly插件逻辑不兼容:即使Parent.pom的pluginManagement中声明了Assembly插件,其打包逻辑与Spring Boot的分层Jar结构不匹配,会导致类路径异常。

具体解决步骤

1. 清理Parent.pom的插件配置

删除Parent.pom中所有与spring-boot-maven-plugin、maven-assembly-plugin相关的配置,包括pluginManagement内的Assembly插件声明。Parent.pom仅保留模块管理、依赖管理等核心配置即可。

2. 在controller-service模块配置Spring Boot插件

主类和启动逻辑位于controller-service模块,因此需在该模块的pom.xml中添加Spring Boot插件配置:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                    <configuration>
                        <mainClass>de.app2.App2BasisServiceApplication</mainClass>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

3. 确认模块依赖关系

确保controller-service模块的pom.xml中已正确依赖basis-service模块:

<dependencies>
    <dependency>
        <groupId>你的项目groupId</groupId>
        <artifactId>basis-service</artifactId>
        <version>你的项目版本号</version>
    </dependency>
    <!-- 其他Spring Boot基础依赖 -->
</dependencies>

4. 执行打包命令

在项目根目录(main-module)下运行Maven打包命令:

mvn clean package

打包完成后,前往controller-service/target目录,会生成两个Jar文件:

  • 不带特殊后缀的普通Jar(仅包含当前模块代码)
  • 带-spring-boot.jar或-exec.jar后缀的可执行Jar(包含所有依赖模块和第三方库)

5. 验证并运行

运行可执行Jar:

java -jar controller-service-xxx-spring-boot.jar

可通过以下命令检查Jar内结构,确认业务类是否存在:

jar tf controller-service-xxx-spring-boot.jar | grep "de/app2/App2BasisServiceApplication.class"

补充说明

Spring Boot的repackage目标会自动处理多模块依赖,将所有依赖模块的Jar打包到可执行Jar的BOOT-INF/lib目录,当前模块的类放入BOOT-INF/classes,并生成正确的启动清单,无需额外使用Assembly插件。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 13:55:23