You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何从多模块Maven项目打包多个含依赖的可执行JAR

解决多模块Maven项目生成独立可执行胖JAR的方案

这问题我太熟了,之前刚帮团队处理过几乎一模一样的多模块打包场景!针对你的需求,给你两个实用的Maven插件方案,完美适配foo-app和bar-app各自生成独立可执行JAR,还能处理依赖冲突的问题:

方案一:通用场景用maven-shade-plugin(非Spring项目首选)

这个插件是Apache官方的,能把所有依赖打包进一个可执行JAR,还能灵活处理依赖冲突。你只需要在foo-app和bar-app各自的POM文件里分别配置就行,common-lib保持普通JAR打包不用改。

具体配置示例(以foo-app为例)

在foo-app的<build>标签下添加插件配置:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.4.1</version> <!-- 选一个稳定的版本即可 -->
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <!-- 指定可执行JAR的主类 -->
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>com.example.foo.FooMainClass</mainClass> <!-- 替换成你的实际主类全路径 -->
                            </transformer>
                        </transformers>
                        <!-- 处理依赖冲突:这里提供两种常见方式 -->
                        <filters>
                            <!-- 通用过滤:排除签名文件避免打包报错 -->
                            <filter>
                                <artifact>*:*</artifact>
                                <excludes>
                                    <exclude>META-INF/*.SF</exclude>
                                    <exclude>META-INF/*.DSA</exclude>
                                    <exclude>META-INF/*.RSA</exclude>
                                </excludes>
                            </filter>
                            <!-- 示例:如果foo-app需要保留特定版本的依赖,排除其他冲突版本 -->
                            <filter>
                                <artifact>com.example.conflict:conflict-artifact</artifact>
                                <includes>
                                    <include>**/*</include>
                                </includes>
                            </filter>
                        </filters>
                        <!-- 进阶:如果冲突严重,还可以用relocate重命名冲突类的包路径 -->
                        <!--
                        <relocations>
                            <relocation>
                                <pattern>com.example.conflict</pattern>
                                <shadedPattern>com.example.foo.shaded.conflict</shadedPattern>
                            </relocation>
                        </relocations>
                        -->
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

bar-app的配置和上面几乎一样,只需要把<mainClass>改成bar-app的主类,再根据bar-app的依赖冲突调整filters或者relocations就行。

方案二:Spring Boot项目用spring-boot-maven-plugin(更省心)

如果你的foo-app和bar-app是Spring Boot应用,那用官方的插件更方便,它默认就会生成包含所有依赖的可执行JAR,处理依赖的逻辑也更贴合Spring生态。

具体配置示例(以bar-app为例)

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>2.7.15</version> <!-- 对应你的Spring Boot版本 -->
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                    <configuration>
                        <!-- 指定主类 -->
                        <mainClass>com.example.bar.BarApplication</mainClass>
                        <!-- 排除冲突依赖:直接移除不需要的版本 -->
                        <excludes>
                            <exclude>
                                <groupId>com.example.conflict</groupId>
                                <artifactId>conflict-artifact</artifactId>
                            </exclude>
                        </excludes>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

最后执行打包

在项目根目录(父POM所在目录)执行:

mvn clean package

执行完成后,foo-app和bar-app的target目录下会生成两个JAR:

  • original-前缀的是轻量JAR(和之前的一样)
  • 不带前缀的就是包含所有依赖的可执行JAR,直接用java -jar xxx.jar就能运行

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

火山引擎 最新活动