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 閱讀(35484) 評論(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內核

          搜索

          •  

          積分與排名

          • 積分 - 319560
          • 排名 - 177

          最新評論

          閱讀排行榜

          主站蜘蛛池模板: 团风县| 扎赉特旗| 五大连池市| 元阳县| 仙游县| 五峰| 前郭尔| 阿尔山市| 安泽县| 额尔古纳市| 樟树市| 牡丹江市| 柳林县| 汉中市| 武穴市| 临朐县| 定州市| 宣汉县| 丰镇市| 灵川县| 房产| 永新县| 涪陵区| 沁源县| 松江区| 鹤峰县| 江北区| 新宾| 松原市| 邳州市| 东海县| 遂川县| 霍山县| 治县。| 临沭县| 宜城市| 金山区| 大宁县| 沐川县| 嵩明县| 桐乡市|