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

Maven多模块项目的target目录管理及依赖问题咨询

多模块Maven项目依赖与打包解决方案

一、将common模块二进制类整合至service的target目录

可以通过Maven插件实现这个需求,推荐使用maven-dependency-plugin的copy目标,在编译阶段自动把common模块的class文件复制到service的target/classes目录中。

在service模块的pom.xml中添加以下插件配置:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-common-classes</id>
                    <!-- 绑定到compile阶段,编译时自动执行 -->
                    <phase>compile</phase>
                    <goals>
                        <goal>copy</goal>
                    </goals>
                    <configuration>
                        <artifactItems>
                            <artifactItem>
                                <!-- 替换为你的项目groupId、common模块的artifactId和版本号 -->
                                <groupId>your-project-groupId</groupId>
                                <artifactId>common</artifactId>
                                <version>your-project-version</version>
                                <type>jar</type>
                                <!-- 指定复制到service的target/classes目录 -->
                                <outputDirectory>${project.build.directory}/classes</outputDirectory>
                                <!-- 只复制class文件 -->
                                <includes>**/*.class</includes>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

执行mvn compile后,common模块的class文件就会被整合到service的target/classes目录中。注意这种方式可能会导致类重复问题,后续打包时需留意是否有冲突。

二、打包依赖common模块的模块处理方式

Maven多模块项目的核心是依赖管理,正确配置依赖后,打包流程会自动处理common模块的引用:

1. 基础依赖配置

首先在service或client-app模块的pom.xml中添加对common模块的依赖:

<dependencies>
    <dependency>
        <groupId>your-project-groupId</groupId>
        <artifactId>common</artifactId>
        <version>your-project-version</version>
    </dependency>
</dependencies>

同时确保项目根目录的pom.xml中正确声明所有模块,保证构建顺序:

<modules>
    <module>common</module>
    <module>service</module>
    <module>client-app</module>
</modules>

2. 两种打包场景

场景1:打包为普通依赖jar

直接在模块目录执行mvn clean package,Maven会先编译common模块,再编译当前模块,最终生成的jar仅包含自身class文件,common模块会作为依赖项存在(发布到Maven仓库后,其他项目可通过依赖引用)。

场景2:打包为独立可运行jar(如Spring Boot项目)

如果是Spring Boot服务,使用spring-boot-maven-plugin,它会自动将依赖的common模块打包进最终的可执行jar中(存放于BOOT-INF/lib目录)。配置示例:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

场景3:合并common类到当前jar

如果需要把common模块的class直接合并到当前模块的jar中,使用maven-shade-plugin:

<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>
                        <artifactSet>
                            <includes>
                                <include>your-project-groupId:common</include>
                            </includes>
                        </artifactSet>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

新手注意事项

  • 保持所有模块的groupId和version一致,避免版本冲突;
  • 不要手动复制class文件,尽量依赖Maven插件实现自动化构建,保证构建的可重复性;
  • 执行构建命令时,优先在项目根目录执行,确保模块按依赖顺序编译。

内容的提问来源于stack exchange,提问作者PEREZ MONSIVAIS JOSE DE JESUS

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.11 19:33:15