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

如何构建包含两个子项目源码的Maven父项目Jar包?

实现思路
  • 父项目保持pom打包类型,仅负责聚合子模块,不参与实际打包流程
  • 新增一个专门的聚合子模块(例如命名为all-in-one),依赖所有业务子模块,通过Maven插件将所有子模块的编译产物合并为单个Jar包
  • 可选:若需要包含源码文件,额外配置maven-source-plugin生成包含源码的Jar
示例配置

1. 父项目pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>parent-project</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <modules>
        <module>module1</module>
        <module>module2</module>
        <module>all-in-one</module> <!-- 新增的聚合模块 -->
    </modules>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
</project>

2. 聚合模块(all-in-one)pom.xml

该模块依赖所有业务子模块,通过maven-shade-plugin合并Jar:

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>com.example</groupId>
        <artifactId>parent-project</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <artifactId>all-in-one</artifactId>
    <packaging>jar</packaging>

    <!-- 依赖所有子模块 -->
    <dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>module1</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>module2</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- 合并所有依赖的类到当前Jar -->
            <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 -->
                            <!-- <mainClass>com.example.Main</mainClass> -->
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!-- 可选:生成包含所有源码的Jar包 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>3.2.1</version>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <phase>package</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

3. 执行打包命令

在父项目根目录执行:

mvn clean package

执行完成后,all-in-one/target目录下会生成包含所有子模块类的Jar包(如果配置了source插件,还会生成带-sources后缀的源码Jar)。

注意事项
  • 子模块的打包类型需为jar
  • 如果子模块之间存在依赖冲突,需在maven-shade-plugin中配置冲突解决策略(例如优先保留某个版本的类)
  • 若不需要可执行Jar,可移除mainClass配置

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 22:20:00