如何将指定Maven共享依赖类复制到子项目最终Jar包?
Maven子项目合并共享模块类到Jar包的实现方案
项目结构
父项目pom.xml的模块配置如下:
<modules> <module>my-project-solution-a</module> <module>my-project-solution-b</module> <module>my-project-shared-solution</module> </modules>
其中my-project-shared-solution是my-project-solution-a和my-project-solution-b的通用共享逻辑模块。
my-project-solution-a 依赖配置
<dependencies> <dependency> <groupId>com.my.solution</groupId> <artifactId>my-project-shared-solution</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> </dependency> </dependencies>
my-project-solution-b 依赖配置
<dependencies> <dependency> <groupId>com.my.solution</groupId> <artifactId>my-project-shared-solution</artifactId> </dependency> <dependency> <groupId>org.glassfish.jersey.core</groupId> <artifactId>jersey-server</artifactId> </dependency> </dependencies>
需求说明
构建时需要将my-project-shared-solution的所有类合并到my-project-solution-a和my-project-solution-b的最终Jar包中,但不包含spring-webmvc、jersey-server等第三方依赖类。其他项目引入这两个子项目时,无需额外添加共享模块依赖,但仍需自行引入spring-webmvc或jersey-server等第三方依赖。
实现方案
你之前用maven-dependency-plugin的思路不对,推荐使用maven-shade-plugin实现指定依赖类的合并,同时排除其他第三方依赖。以下是子项目(my-project-solution-a和my-project-solution-b)的pom.xml配置:
子项目通用配置(可放在父pom或各自子pom中)
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.5.1</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <!-- 指定只合并共享模块的类 --> <artifactSet> <includes> <include>com.my.solution:my-project-shared-solution</include> </includes> </artifactSet> <!-- 生成精简pom,自动移除共享模块依赖,保留第三方依赖声明 --> <dependencyReducedPomLocation>${project.build.directory}/dependency-reduced-pom.xml</dependencyReducedPomLocation> <!-- 可选:如果有类冲突可配置转换器,默认即可 --> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <!-- 若为Web项目需指定主类,普通Jar可省略 --> <!-- <mainClass>com.my.solution.a.MainClass</mainClass> --> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build>
配置说明
- maven-shade-plugin的
shade目标会在package阶段执行,将共享模块的类合并到当前项目的Jar中。 artifactSet的<include>仅指定共享模块,确保只合并该模块的类,不会包含其他第三方依赖。dependencyReducedPomLocation会生成精简后的pom.xml,自动移除共享模块的依赖(已合并到Jar),但保留spring-webmvc、jersey-server等第三方依赖的声明。如果需要让用户手动引入第三方依赖,可将这些依赖的<scope>设置为provided。
替代方案(使用maven-dependency-plugin)
如果坚持用maven-dependency-plugin,需分两步:复制共享模块Jar,再将其类打包到当前Jar,步骤如下:
<build> <plugins> <!-- 第一步:复制共享模块Jar到编译目录 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>3.6.1</version> <executions> <execution> <id>copy-shared-jar</id> <phase>prepare-package</phase> <goals> <goal>copy</goal> </goals> <configuration> <artifactItems> <artifactItem> <groupId>com.my.solution</groupId> <artifactId>my-project-shared-solution</artifactId> <version>${project.version}</version> <outputDirectory>${project.build.directory}/classes</outputDirectory> <overWrite>true</overWrite> </artifactItem> </artifactItems> <excludeTransitive>true</excludeTransitive> </configuration> </execution> </executions> </plugin> <!-- 第二步:将共享模块类打包到当前Jar --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.3.0</version> <configuration> <includes> <include>**/*.class</include> </includes> </configuration> </plugin> </plugins> </build>
此方案步骤繁琐,不如maven-shade-plugin简洁,优先推荐第一种方案。
内容的提问来源于stack exchange,提问作者victorio
相关产品推荐
相关产品推荐

