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

如何在Maven中一次性生成同一项目的两种不同模块组合构件

一次性生成两个不同构件的实现方案

分不同主流构建工具给出具体实现方式:

Maven(Java项目)

利用Maven的maven-assembly-plugin配置多个执行任务,分别对应两种打包需求,指定不同的输出文件名:

在项目根pom.xml中添加如下配置:

<build>
    <plugins>
        <!-- 打包Module A + Module B的构件 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.6.0</version>
            <executions>
                <execution>
                    <id>package-full</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                        <finalName>project-full-${project.version}</finalName>
                        <includes>
                            <include>com/example/modulea/**</include>
                            <include>com/example/moduleb/**</include>
                        </includes>
                    </configuration>
                </execution>
                <!-- 仅打包Module B的构件 -->
                <execution>
                    <id>package-b-only</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                        <finalName>project-moduleb-${project.version}</finalName>
                        <includes>
                            <include>com/example/moduleb/**</include>
                        </includes>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

执行mvn package后,target目录会生成两个不同名称的构件:project-full-xxx.jar和project-moduleb-xxx.jar。

Gradle(Java/Kotlin项目)

在根项目的build.gradle中创建两个自定义打包任务,分别配置不同的打包内容:

// 打包Module A + Module B的任务
task packageFull(type: Jar) {
    archiveBaseName.set('project-full')
    archiveVersion.set(project.version)
    // 包含Module A和Module B的编译产物
    from project(':moduleA').sourceSets.main.output
    from project(':moduleB').sourceSets.main.output
    // 打包依赖
    from {
        configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
    }
}

// 仅打包Module B的任务
task packageBOnly(type: Jar) {
    archiveBaseName.set('project-moduleb')
    archiveVersion.set(project.version)
    from project(':moduleB').sourceSets.main.output
    // 打包依赖
    from {
        configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
    }
}

// 将两个任务绑定到build生命周期
build.dependsOn packageFull, packageBOnly

执行gradle build后,build/libs目录会生成两个目标构件。

Webpack(前端项目)

在webpack.config.js中导出多个配置对象,分别对应两种打包需求:

const path = require('path');

module.exports = [
    // 打包Module A + Module B的配置
    {
        entry: {
            main: ['./src/moduleA/index.js', './src/moduleB/index.js']
        },
        output: {
            filename: 'project-full-[contenthash].js',
            path: path.resolve(__dirname, 'dist')
        }
    },
    // 仅打包Module B的配置
    {
        entry: './src/moduleB/index.js',
        output: {
            filename: 'project-moduleb-[contenthash].js',
            path: path.resolve(__dirname, 'dist')
        }
    }
];

执行webpack命令后,dist目录会生成两个不同名称的打包文件。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 22:27:18