maven 循环依赖处理办法

当两个工程 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值