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

如何用Maven将多模块项目打包为单个Jar包?

Maven多模块项目打包为单个Jar包的方案

核心思路

在多模块项目中,选择一个主模块(或新建专门的打包模块),通过Maven插件将所有模块代码及依赖合并为单个Jar包。以下是三种常用方案:


方法一:使用maven-shade-plugin(推荐,处理依赖冲突更优)

该插件能合并所有模块代码和第三方依赖,还可处理META-INF文件冲突,生成可执行Jar。

1. 配置插件(在主模块的pom.xml中)

找到项目的入口主类(如com.example.MainApplication),添加如下配置:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.5.1</version> <!-- 可替换为最新稳定版本 -->
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <!-- 指定可执行Jar的主类 -->
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>com.example.MainApplication</mainClass>
                            </transformer>
                            <!-- 合并Spring相关配置文件,避免冲突 -->
                            <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/spring.handlers</resource>
                            </transformer>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/spring.schemas</resource>
                            </transformer>
                        </transformers>
                        <!-- 排除签名文件,避免打包报错 -->
                        <filters>
                            <filter>
                                <artifact>*:*</artifact>
                                <excludes>
                                    <exclude>META-INF/*.SF</exclude>
                                    <exclude>META-INF/*.DSA</exclude>
                                    <exclude>META-INF/*.RSA</exclude>
                                </excludes>
                            </filter>
                        </filters>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

2. 执行打包命令

在项目根目录或主模块目录下运行:

mvn clean package

打包完成后,主模块的target目录下会生成两个Jar文件,带-shaded后缀的即为包含所有模块和依赖的可执行Jar。


方法二:使用maven-assembly-plugin

该插件配置简单,适合快速生成包含依赖的Jar。

1. 配置插件(在主模块的pom.xml中)

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.6.0</version>
            <configuration>
                <descriptorRefs>
                    <!-- 使用内置的打包规则,包含所有依赖 -->
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <!-- 指定主类 -->
                        <mainClass>com.example.MainApplication</mainClass>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

2. 执行打包命令

同样运行:

mvn clean package

target目录下带-jar-with-dependencies后缀的Jar即为目标文件。


方法三:Spring Boot项目专属方案

如果是Spring Boot多模块项目,直接使用Spring Boot官方插件即可,无需额外处理依赖合并:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>3.2.0</version> <!-- 对应你的Spring Boot版本 -->
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

执行mvn clean package后,target目录下的Jar就是包含所有模块和依赖的可执行Jar。


注意事项

  • 确保主模块的pom.xml中已正确引入所有需要打包的子模块依赖,例如:
<dependencies>
    <dependency>
        <groupId>你的项目groupId</groupId>
        <artifactId>子模块1-artifactId</artifactId>
        <version>项目版本号</version>
    </dependency>
    <dependency>
        <groupId>你的项目groupId</groupId>
        <artifactId>子模块2-artifactId</artifactId>
        <version>项目版本号</version>
    </dependency>
</dependencies>
  • 若打包时报错,优先检查依赖冲突或主类路径是否正确。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 10:25:21