您好,登錄后才能下訂單哦!
maven中如何使用聚合模塊,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
聚合模塊為項目目錄的最頂層,其他模塊作為聚合模塊子目錄而存在。目的是為一次構建多個項目模塊。
我自己化了一個關于項目的關系:
maven-aggregate作為maven pom項目,需要定義pom.xml的 packaging 為 pom類型
我們還是參照 maven的依賴特性,沖突解決(五) 創建 maven-01 maven-02 maven-03工程,通過maven-aggregate pom工程 對這三個maven項目做一個聚合,配置如下:
Xml代碼
1. <project xmlns="http: maven.apache.org pom 4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2. xsi:schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 3. <modelversion>4.0.0</modelversion> 5. <groupid>com.sohu.train</groupid> 6. <artifactid>maven-aggregate</artifactid> 7. <version>0.0.1-SNAPSHOT</version> 8. <packaging>pom</packaging> 10. <!-- 子模塊 指定項目pom的位置 ..代表上一級目錄--> 11. <modules> 12. <module>../maven-01</module> 13. <module>../maven-02</module> 14. <module>..maven-03</module> 15. </modules> 17.
對 maven-aggregate執行下mvn clean compile
Xml代碼
1. \[INFO\] Scanning for projects... 2. \[WARNING\] 3. \[WARNING\] Some problems were encountered while building the effective model for com.sohu.train:maven-02:jar:1.0-SNAPSHORT 4. \[WARNING\] 'dependencies.dependency.exclusions.exclusion.groupId' for junit:junit:jar is missing. @ line 38, column 16 5. \[WARNING\] 'dependencies.dependency.exclusions.exclusion.artifactId' for junit:junit:jar is missing. @ line 38, column 16 6. \[WARNING\] 7. \[WARNING\] It is highly recommended to fix these problems because they threaten the stability of your build. 8. \[WARNING\] 9. \[WARNING\] For this reason, future Maven versions might no longer support building such malformed projects. 10. \[WARNING\] 11. \[INFO\] ------------------------------------------------------------------------ 12. \[INFO\] Reactor Build Order: //構建 maven 的反應堆,執行清理 編譯的順序 13. \[INFO\] 14. \[INFO\] maven-02 15. \[INFO\] maven-01 16. \[INFO\] maven-03 17. \[INFO\] maven-aggregate 18. \[INFO\] 19. \[INFO\] ------------------------------------------------------------------------ 20. \[INFO\] Building maven-02 1.0-SNAPSHORT 21. \[INFO\] ------------------------------------------------------------------------ 22. \[INFO\] 23. \[INFO\] --- maven-clean-plugin:2.5:clean (default-clean) @ maven-02 --- 24. \[INFO\] Deleting E:\\train\\test\\maven-02\\target 25. \[INFO\] 26. \[INFO\] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-02 --- 27. \[INFO\] Using 'UTF-8' encoding to copy filtered resources. 28. \[INFO\] skip non existing resourceDirectory E:\\train\\test\\maven-02\\src\\main\\resources 29. \[INFO\] 30. \[INFO\] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-02 --- 31. \[INFO\] Changes detected - recompiling the module! 32. \[INFO\] Compiling 1 source file to E:\\train\\test\\maven-02\\target\\classes 33. \[INFO\] 34. \[INFO\] ------------------------------------------------------------------------ 35. \[INFO\] Building maven-01 1.0-SNAPSHOT 36. \[INFO\] ------------------------------------------------------------------------ 37. \[INFO\] 38. \[INFO\] --- maven-clean-plugin:2.5:clean (default-clean) @ maven-01 --- 39. \[INFO\] Deleting E:\\train\\test\\maven-01\\target 40. \[INFO\] 41. \[INFO\] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-01 --- 42. \[INFO\] Using 'UTF-8' encoding to copy filtered resources. 43. \[INFO\] Copying 1 resource 44. \[INFO\] 45. \[INFO\] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-01 --- 46. \[INFO\] Changes detected - recompiling the module! 47. \[INFO\] Compiling 1 source file to E:\\train\\test\\maven-01\\target\\classes 48. \[INFO\] 49. \[INFO\] ------------------------------------------------------------------------ 50. \[INFO\] Building maven-03 0.0.1-SNAPSHOT 51. \[INFO\] ------------------------------------------------------------------------ 52. \[INFO\] 53. \[INFO\] --- maven-clean-plugin:2.5:clean (default-clean) @ maven-03 --- 54. \[INFO\] Deleting E:\\train\\test\\maven-03\\target 55. \[INFO\] 56. \[INFO\] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-03 --- 57. \[INFO\] Using 'UTF-8' encoding to copy filtered resources. 58. \[INFO\] skip non existing resourceDirectory E:\\train\\test\\maven-03\\src\\main\\resources 59. \[INFO\] 60. \[INFO\] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-03 --- 61. \[INFO\] Changes detected - recompiling the module! 62. \[INFO\] Compiling 1 source file to E:\\train\\test\\maven-03\\target\\classes 63. \[INFO\] 64. \[INFO\] ------------------------------------------------------------------------ 65. \[INFO\] Building maven-aggregate 0.0.1-SNAPSHOT 66. \[INFO\] ------------------------------------------------------------------------ 67. \[INFO\] 68. \[INFO\] --- maven-clean-plugin:2.5:clean (default-clean) @ maven-aggregate --- 69. \[INFO\] ------------------------------------------------------------------------ 70. \[INFO\] Reactor Summary: // 可以看到 對3個maven項目 一起執行編譯 71. \[INFO\] 72. \[INFO\] maven-02 ........................................... SUCCESS \[ 1.874 s\] 73. \[INFO\] maven-01 ........................................... SUCCESS \[ 0.108 s\] 74. \[INFO\] maven-03 ........................................... SUCCESS \[ 0.158 s\] 75. \[INFO\] maven-aggregate .................................... SUCCESS \[ 0.002 s\] 76. \[INFO\] ------------------------------------------------------------------------ 77. \[INFO\] BUILD SUCCESS 78. \[INFO\] ------------------------------------------------------------------------ 79. \[INFO\] Total time: 2.263 s 80. \[INFO\] Finished at: 2016-01-15T14:35:41+08:00 81. \[INFO\] Final Memory: 14M/227M 82. \[INFO\] ------------------------------------------------------------------------
這樣我們就完成 maven對子模塊的聚合 ,像我們平常用的dubbo jetty tomcat spring hibernate等等 都是按模塊 去開發,一次編譯 各個模塊都能緊密聯系在一起了。我們在平常開發項目也需要適當的去分模塊開發,
這是dubbo的子模塊,分了很多子模塊:
pom繼承是為了抽出重復配置,通常配置在父模塊中,為子模塊提供使用,這樣可以做到“一處聲明,處處使用”。
1.常用的pom被繼承的元素
Xml代碼
1. groupId :項目組 ID ,項目坐標的核心元素; 2. version :項目版本,項目坐標的核心元素; 3. description :項目的描述信息; 4. properties :自定義的 Maven 屬性; 5. dependencies :項目的依賴配置; 6. dependencyManagement :醒目的依賴管理配置; 7. repositories :項目的倉庫配置; 8. build :包括項目的源碼目錄配置、輸出目錄配置、插件配置、插件管理配置等;
1.配置maven-aggregate pom文件:
Xml代碼
1. <project xmlns="http: maven.apache.org pom 4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2. xsi:schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 3. <modelversion>4.0.0</modelversion> 5. <groupid>com.sohu.train</groupid> 6. <artifactid>maven-aggregate</artifactid> 7. <version>0.0.1-SNAPSHOT</version> 8. <packaging>pom</packaging> 10. <!-- 子模塊 --> 11. <modules> 12. <module>../maven-01</module> 13. <module>../maven-02</module> 14. <module>../maven-03</module> 15. </modules> 16. <!-- 統一配置構件的版本號 --> 17. <properties> 18. <junit.version>3.8.1</junit.version> 19. </properties> 21. <!-- 依賴管理 --> 22. <dependencymanagement> 23. <dependencies> 24. <dependency> 25. <groupid>junit</groupid> 26. <artifactid>junit</artifactid> 27. <version>${junit.version}</version> 28. <scope>test</scope> 29. </dependency> 30. </dependencies> 31. </dependencymanagement> 32.
maven-01,maven-02,maven-03 需要配置parent ,dependencies,同時對junit做一個依賴,3個工程的pom配置如下:
Xml代碼
1. <project xmlns="http: maven.apache.org pom 4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2. xsi:schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 3. <modelversion>4.0.0</modelversion> 4. <!-- 指定父pom的坐標及pom位置 --> 5. <parent> 6. <groupid>com.sohu.train</groupid> 7. <artifactid>maven-aggregate</artifactid> 8. <version>0.0.1-SNAPSHOT</version> 9. <relativepath>../maven-aggregate/pom.xml</relativepath> 10. </parent> 11. <artifactid>maven-03</artifactid> 12. <packaging>jar</packaging> 13. <!-- 添加對junit依賴,這樣公用配置只需要在maven-aggregate中去配置 --> 14. <dependencies> 15. <dependency> 16. <groupid>junit</groupid> 17. <artifactid>junit</artifactid> 18. </dependency> 19. </dependencies> 20.
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。