Maven項目常見問題實戰(一) 解決相互依賴
很多時候隨著項目的膨脹,模塊會越來越多,如果設計上 稍有不慎就會出現模塊之間相互依賴的情況。這對于使用Maven的用戶是比較痛苦的,因為出現模塊之間相互依賴的話在構建的時候就會失敗,Maven通常要先編譯被依賴的模塊,如果出現相互依賴Maven就不知道該怎么辦了。下圖描述了三個Maven模塊相互依賴的場景:

圖中模塊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解決相互依賴的問題
我的解決辦法就是先把相互依賴的模塊整合在一起,相當于把這些模塊合并成一個單獨的模塊統一編譯,如下圖:

這樣就產生了一個合并模塊D,我們把它當做一個輔助構建模塊,然后讓A、B、C模塊都依賴于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模型如下:
<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>
這時候你在運行例子就能夠成功構建了!
posted on 2011-06-28 12:40 kuuyee 閱讀(35484) 評論(3) 編輯 收藏 所屬分類: Git/Maven 、JEE