maven中如何解决子模块循环依赖的问题
·
正常情况下,循环依赖是很少见的,当一个程序包含多个模块,模块之间就可能出现循环依赖,一般情况下,根据错误信息也能解决循环依赖。
本文描述一个实际工作中,解决子模块循环依赖的问题
1. 原因
工作中,基于一个开源项目进行扩展,增加了数据融合相关的逻辑,并把这一块单独放在一个子模块A中。
但子模块A需要用到子模块core中定义的一些接口,反过来,子模块core启动时,需要把子模块A实现的逻辑加入,造成子模块A依赖于子模块core,core依赖于A的循环依赖问题。
2. 解决方案
build-helper-maven-plugin插件允许引入额外路径的源码和资源文件–如其他module下的源文件,相关的goals详见官网, 部分goal如下,本文使用1、5两个goal
-
添加额外的源码路径
-
添加额外的测试源码路径
-
添加额外的资源路径
-
添加额外的测试资源路径
-
附加其它jar,如子模块
2.1 子模块A的pom.xml调整
子模块A只在编译期间依赖于A
注意:scope范围是provided
<dependency>
<groupId>com.xxx</groupId>
<artifactId>core</artifactId>
<version>${project-version}</version>
<scope>provided</scope>
</dependency>
2.2 子模块core的pom.xml调整
通过build-helper-maven-plugin插件,将子模块A的的源码添加到子模块core额外编译目录中,在package时,直接将子模块A的代码打到子模块的jar中
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>../module-a/src/main/java</source>
</sources>
</configuration>
</execution>
<execution>
<id>attach-module-a-artifact</id>
<phase>package</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<!-- 将代码打包到子模块core jar中了-->
<file>../module-a/target/module-a-${project.version}.jar</file>
<type>jar</type>
<classifier>module-a</classifier>
</artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
更多推荐
所有评论(0)