Maven项目常见问题实战(一) 解决相互依赖

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

Company Logo

图中模块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 switch.
[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 read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/ProjectCycleException

使用build-helper-maven-plugin解决相互依赖的问题

我的解决办法就是先把相互依赖的模块整合在一起,相当于把这些模块合并成一个单独的模块统一编译,如下图:

Company Logo

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

Company Logo

要想把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模型如下:

<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>

这时候你在运行例子就能够成功构建了!

2011-06-28

posted on 2011-06-28 12:40 kuuyee 阅读(35171) 评论(3)  编辑  收藏 所属分类: Git/MavenJEE

评论

# re: Maven项目常见问题实战(一) 解决相互依赖 2011-06-28 20:49 bryan

自己的项目是通过重构类来解决这个问题的.原来是可以这样的,谢谢  回复  更多评论   

# re: Maven项目常见问题实战(一) 解决相互依赖[未登录] 2014-01-09 18:30 kevin

A,B,C在依赖于D时,在eclipse中还是会报错。为什么?
查看ABC各自己的依赖树,发现D的Classes内所以的文件都未出在ABC引用。
Help!  回复  更多评论   

# re: Maven项目常见问题实战(一) 解决相互依赖[未登录] 2014-01-09 18:33 kuuyee

只能说你的Maven POM文件配置还不对@kevin
  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 

导航

<2011年6月>
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789

统计

随笔分类(139)

Linux内核

搜索

积分与排名

最新评论

阅读排行榜