maven相互依赖

很​多​时​候​随​着​项​目​的​膨​胀​,模​块​会​越​来​越​多​,如​果​设​计​上​ 稍​有​不​慎​就​会​出​现​模​块​之​间​相​互​依​赖​的​情​况​。​这​对​于​使​用​Maven的​用​户​是​比​较​痛​苦​的​,因​为​出​现​模​块​之​间​相​互​依​赖​的​话​在​构​建​的​时​候​就​会​失​败​,Maven通​常​要​先​编​译​被​依​赖​的​模​块​,如​果​出​现​相​互​依​赖​Maven就​不​知​道​该​怎​么​办​了​。​下​图​描​述​了​三​个​Maven模​块​相​互​依​赖​的​场​景​: 

图 1. A、​B、​C三​个​模​块​相​互​依​赖​


图​中​模​块​C依​赖​于​模​块​B,模​块​B依​赖​于​模​块​A,而​模​块​A又​依​赖​于​模​块​C,这​样​就​出​现​了​相​互​依​赖​情​况​,如​果​运​行​mvn compile会​出​现​如​下​错​误​:
[INFO] Scanning for projects... [ERROR] The projects in the reactor contain a cyclic reference: Edge between 'Ve rtex{label='org.kuuyee.sample:module-C:1.0-SNAPSHOT'}' and 'Vertex{label='org.ku uyee.sample:module-B:1.0-SNAPSHOT'}' introduces to cycle in the graph org.kuuyee .sample:module-B:1.0-SNAPSHOT --> org.kuuyee.sample:module-A:1.0-SNAPSHOT --> or g.kuuyee.sample:module-C:1.0-SNAPSHOT --> org.kuuyee.sample:module-B:1.0-SNAPSHO T -> [Help 1][ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e swit ch.[ERROR] Re-run Maven using the -X switch to enable full debug logging.[ERROR] [ERROR] For more information about the errors and possible solutions, please rea d the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/ProjectCycleEx ception
1. 使​用​build-helper-maven-plugin解​决​相​互​依​赖​的​问​题​我​的​解​决​办​法​就​是​先​把​相​互​依​赖​的​模​块​整​合​在​一​起​,相​当​于​把​这​些​模​块​合​并​成​一​个​单​独​的​模​块​统​一​编​译​,
如​下​图​:图 2. 合​并​A、​B、​C三​个​模​块​为​D模​块​


这​样​就​产​生​了​一​个​合​并​模​块​D,我​们​把​它​当​做​一​个​辅​助​构​建​模​块​,然​后​让​A、​B、​C模​块​都​依​赖​于​D模​块​,这​样​的​话​就​可​以​成​功​编​译​A、​B和​C模​块​,
如​下​图​: 图 3. 基​于​D模​块​来​分​别​编​译​A、​B、​C三​个​模​块​

要​想​把​A、​B、​C三​个​模​块​整​合​在​一​起​编​译​,需​要​借​助​build-helper-maven-plugin插​件​,这​个​插​件​在​Maven构​建​周​期​提​供​一​些​辅​助​功​能​,下​面​列​出​插​件​的​提​供​的​功​能​列​表​: build-helper:add-source:添​加​更​多​的​构​建​源​码​目​录​ build-helper:add-test-source:添​加​更​多​的​测​试​源​码​目​录​ build-helper:add-resource:添​加​更​多​的​资​源​目​录​ build-helper:add-test-resource:添​加​更​多​的​测​试​资​源​目​录​ build-helper:attach-artifact:在​安​装​和​部​署​周​期​附​加​artifacts build-helper:maven-version:添​加​一​个​指​定​当​前​Maven版​本​的​属​性​ build-helper:parse-version:添​加​一​个​指​定​组​件​版​本​的​属​性​ build-helper:released-version:决​定​当​前​项​目​的​最​终​版​本​ build-helper:remove-project-artifact:从​本​地​资​源​库​中​移​除​项​目​的​artifacts build-helper:reserve-network-port:Reserve a list of random and unused network ports. 在​这​里​我​们​要​用​到​build-helper:add-source这​个​功​能​,将​模​块​A、​B、​C的​源​码​路​径​加​进​来​。​ 我​们​再​添​加​一​个​辅​助​模​块​D,在​辅​助​模​块​D中​使​用​build-helper-maven-plugin插​件​,然​后​让​模​块​A、​B、​C都​依​赖​于​辅​助​模​块​D,模​块​D的​POM模​型​如​下​: 例 1. 辅​助​模​块​D的​POM模​型​

Java代码   收藏代码
  1. <project xmlns="http://maven.apache.org/POM/4.0.0"  
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  4.     <parent>  
  5.         <groupId>org.kuuyee.sample</groupId>  
  6.         <artifactId>sample-parent</artifactId>  
  7.         <version>1.0-SNAPSHOT</version>  
  8.         <relativePath>../../pom.xml</relativePath>  
  9.     </parent>  
  10.     <modelVersion>4.0.0</modelVersion>  
  11.     <groupId>org.kuuyee.sample</groupId>  
  12.     <artifactId>module-D</artifactId>  
  13.     <version>1.0-SNAPSHOT</version>  
  14.     <packaging>jar</packaging>  
  15.     <name>module-D</name>  
  16.     <url>http://maven.apache.org</url>  
  17.     <properties>  
  18.         <project.build.sourceEncoding>  
  19.             UTF-8  
  20.         </project.build.sourceEncoding>  
  21.         <module.a.src>../../module/module-A/src/main/java</module.a.src>  
  22.         <module.b.src>../../module/module-B/src/main/java</module.b.src>  
  23.         <module.c.src>../../module/module-C/src/main/java</module.c.src>  
  24.     </properties>  
  25.     <build>  
  26.         <plugins><!-- 解决模块相互依赖,综合所有相互依赖代码统一编译 -->  
  27.             <plugin>  
  28.                 <groupId>org.codehaus.mojo</groupId>  
  29.                 <artifactId>build-helper-maven-plugin</artifactId>  
  30.                 <executions>  
  31.                     <execution>  
  32.                         <id>add-source</id>  
  33.                         <phase>generate-sources</phase>  
  34.                         <goals>  
  35.                             <goal>add-source</goal>  
  36.                         </goals>  
  37.                         <configuration>  
  38.                             <sources>  
  39.                                 <source>${module.a.src}</source>  
  40.                                 <source>${module.b.src}</source>  
  41.                                 <source>${module.c.src}</source>  
  42.                             </sources>  
  43.                         </configuration>  
  44.                     </execution>  
  45.                 </executions>  
  46.             </plugin>  
  47.         </plugins>  
  48.     </build>  
  49.     <dependencies>  
  50.         <dependency>  
  51.             <groupId>junit</groupId>  
  52.             <artifactId>junit</artifactId>  
  53.             <version>3.8.1</version>  
  54.             <scope>test</scope>  
  55.         </dependency>  
  56.     </dependencies>  
  57. </project>  
<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<parent>
		<groupId>org.kuuyee.sample</groupId>
		<artifactId>sample-parent</artifactId>
		<version>1.0-SNAPSHOT</version>
		<relativePath>../../pom.xml</relativePath>
	</parent>
	<modelVersion>4.0.0</modelVersion>
	<groupId>org.kuuyee.sample</groupId>
	<artifactId>module-D</artifactId>
	<version>1.0-SNAPSHOT</version>
	<packaging>jar</packaging>
	<name>module-D</name>
	<url>http://maven.apache.org</url>
	<properties>
		<project.build.sourceEncoding>
			UTF-8
		</project.build.sourceEncoding>
		<module.a.src>../../module/module-A/src/main/java</module.a.src>
		<module.b.src>../../module/module-B/src/main/java</module.b.src>
		<module.c.src>../../module/module-C/src/main/java</module.c.src>
	</properties>
	<build>
		<plugins><!-- 解决模块相互依赖,综合所有相互依赖代码统一编译 -->
			<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}</source>
								<source>${module.b.src}</source>
								<source>${module.c.src}</source>
							</sources>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
</project>


【转载地址】http://www.blogjava.net/kuuyee/archive/2011/06/28/353158.html

maven处理循环依赖
 在多maven工程的项目里,如果工程间存在循环依赖,构建就会报错。本文介绍一下循环依赖要怎么处理
  
  1、什么是循环依赖
  
  如果工程A依赖工程B,工程B又依赖工程A,就会形成循环依赖。或者A依赖B,B依赖C,C依赖A,也是循环依赖
  
  总的来说,在画出工程依赖图之后,如果发现工程间的依赖连线形成了一个有向循环图,则说明有循环依赖的现象
  
  如果循环依赖发生在工程之间,则会影响构建,因为maven不知道应该先编译哪个工程。如果循环依赖发生在同一个工程的模块之间,虽然不影响编译,但是也是一种不好的实践,说明模块的设计有问题,应该避免
  
  如果在模块内部,有几个类互相调用的话,我觉得可能是正常的。比如观察者模式里面,Observer和Observable就是互相依赖的
  
  2、怎么解决循环依赖
  
  目前知道有2个办法可以解决
  
  第一个办法是用build-helper-maven-plugin插件来规避。比如A依赖B,B依赖C,C依赖A的情况。这个插件提供了一种规避措施,即临时地将工程A、B、C合并成一个中间工程,编译出临时的模块D。然后A、B、C再分别依赖临时模块D进行编译
  
  这种方法可以解决无法构建的问题,但是只是一个规避措施,工程的依赖关系依然是混乱的
  
  第二个办法是通过重构,从根本上消除循环依赖
  
  3、如何重构
  
  目前也知道2个重构的思路
  
  第一个办法是平移,比如A和B互相依赖,那么可以将B依赖A的那部分代码,移动到工程B中,这样一来,B就不需要继续依赖A,只要A依赖B就可以了,从而消除循环依赖
  
  第二个办法是下移,比如A和B互相依赖,同时它们都依赖C,那么可以将B和A相互依赖的那部分代码,移动到工程C里,这样一来,A和B相互之间都不依赖,只继续依赖C,也可以消除循环依赖
  
  这两种重构方式都是可行的,具体采用哪种方式要根据实际情况来判断。不管采取哪种方式,都需要对代码进行修改,有时候并不是那么容易的

猜你喜欢

转载自weitao1026.iteye.com/blog/2336751
今日推荐