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

如何配置Maven Assembly为多模块项目生成带入口的Fat Jar

问题分析与解决方案

报错根源

父模块是聚合/父POM模块,本身没有业务代码和编译后的class文件。你在父模块中同时配置了jar-with-dependencies描述符,插件尝试为父模块构建包含依赖的Jar时,因为父模块没有可打包的内容,就会抛出“archive can not be empty”错误。

正确配置方案

需要将文件分发包和可运行Fat Jar的配置分开:父模块负责生成文件分发包,childModule2(含入口类的业务模块)单独配置生成Fat Jar。

1. 修改父模块的assembly插件配置

移除jar-with-dependencies描述符和archive相关配置,只保留文件分发包的构建逻辑:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <descriptors>
            <descriptor>assembly/assembly.xml</descriptor>
        </descriptors>
        <!-- 移除jar-with-dependencies和archive配置 -->
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

2. 在childModule2中配置Fat Jar生成

在childModule2的pom.xml中添加独立的assembly插件配置,专门生成包含所有依赖的可运行Jar:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>com.myapps.application.App</mainClass>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <id>build-fat-jar</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

关键说明

  • 不需要在父模块同时配置两种描述符:父模块的核心作用是聚合子模块、统一管理依赖和插件,本身无业务代码,不适合生成可运行Jar。
  • childModule2是业务入口模块,依赖childModule1,在这里配置Fat Jar生成逻辑更合理,插件会自动将自身代码和所有依赖(包括childModule1)打包到同一个Jar中。
  • 你的assembly.xml已经能正常生成文件分发包,无需修改。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 21:54:57