当两个工程 B、C 互相依赖时,有两条解决办法
1、将依赖部分独立出来,B-used-by-C、C-used-by-B 创建新的工程D,B和C都引用D
2、将 B 和 C 工程合并成新的工程 AB
3、临时办法,将 C 打包成 JAR 包,在 B 中以 systemPath 方式引入;C 中依赖配置不变。
下面详细说明一下方法3的配置和注意点
1、将C工程导出,从 eclipse 导出时,记得勾选 “Add directory entries”(当使用 ClassPathXmlApplicationContext 做单元测试时,如果不加该选项,会报 org.springframework.beans.factory.UnsatisfiedDependencyException 异常)
当 java 目录有 xml 等其它格式文件时,勾选“Export all output folders for checked projects”才会打包成 jar 包。
2、用 jar -tf modulec.jar 检查打包后的目录是否编译后输出的包结构
3、 b 工程 pom.xml 中修改依赖
<dependency>
<groupId>com.test</groupId>
<artifactId>modulec</artifactId>
<version>0.0.1</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/modulec.jar</systemPath>
</dependency>
4、使用 maven 打包 jar 时设置 addDefaultImplementationEntries 为 true
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.2</version>
<executions>
<execution>
<id>service-jar</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
</archive>
<classifier>bak</classifier>
<includes>
<include>com/**</include>
<include>*.properties</include>
<include>*.xml</include>
</includes>
<excludes>
<exclude>**/test/**</exclude>
<exclude>log4j.properties</exclude>
</excludes>
</configuration>
</plugin>
5、使用 jar 打包
jar cvf test.jar -C classes .
zip -d test.jar test/*
参考资料:
1. How to resolve cyclic dependency in Maven?. https://stackoverflow.com/questions/16468525/how-to-resolve-cyclic-dependency-in-maven
2. 使用eclipse export打jar包,web工程使用*读取配置读不到的问题解决方法. https://blog.csdn.net/wumingabc/article/details/7441232