多模塊實際案例
project
|--business (核心業務) |--business-api |--business-service |--business-message |--business-dao |--business-web |--common (公共組件、服務、常量) |--common-component |--common-component-... |--common-service |--common-constants |--common-... |--management (管理台) |--management-... |--taskserver (定時任務、批處理) |--msgserver (消息隊列)
示例一
Maven多模塊項目
Maven多模塊項目,適用於一些比較大的項目,通過合理的模塊拆分,實現代碼的復用,便於維護和管理。尤其是一些開源框架,也是采用多模塊的方式,提供插件集成,用戶可以根據需要配置指定的模塊。
項目結構如下:
test-hd-parent (父級) ---pom.xml ---test-hd-api (第三方接口層) ----pom.xml ---test-hd-foundation (基礎工具層) ----pom.xml ---test-hd-resource (資源層) ----pom.xml ---test-hd-service (邏輯業務層) ----pom.xml ---test-hd-modules (web層) ----pom.xml ---test-hd-www (web模塊1) ----pom.xml ---test-hd-admin (web模塊2) ----pom.xml
創建一個父maven工程
- 新建一個maven項目,選擇存儲位置,並選擇創建一個簡單的maven工程
- 輸入Group Id、Artifact Id、Packaging,packaging選擇pom包
- 生成父工程,pom.xml如下
- 刪除工程中的src 目錄
創建子模塊
- 右擊父工程名---》New---》Project,然后選擇新建一個maven module工程
- 設置子工程名以及父工程,再設置快速創建模式
- 得到子工程(test-hd-api,第三方接口層),設置編譯的jdk
- 同理設置,子模塊:test-hd-foundation(基礎工具層)、test-hd-resource(資源層) 、test-hd-service(邏輯業務層)
- 新建test-hd-modules (web層),選擇創建一個a simple project,輸入Group Id、Artifact Id、Packaging,packaging選擇pom包
創建web子模塊
- web子模塊在建在test-hd-modules (web層)里面,右擊test-hd-modules 工程名---》New---》Project,然后選擇新建一個maven module工程,設置子工程名以及父工程,選擇新建web項目
- 配置maven web項目,參照:【Maven】Eclipse 使用Maven創建Java Web項目
- 同理可以配置其他的web子模塊 test-hd-admin(web模塊2)
配置個模塊的依賴
- 在parent項目pom.xml中建立依賴管理(dependencyManagement)
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 <groupId>com.hd</groupId> 5 <artifactId>test-hd-parent</artifactId> 6 <version>0.0.1-SNAPSHOT</version> 7 <packaging>pom</packaging> 8 <modules> 9 <module>test-hd-api</module> 10 <module>test-hd-service</module> 11 <module>test-hd-resource</module> 12 <module>test-hd-foundation</module> 13 <module>test-hd-modules</module> 14 </modules> 15 16 17 <!-- maven依賴 --> 18 <dependencyManagement> 19 20 <dependencies> 21 <!-- hd --> 22 <dependency> 23 <groupId>com.hd</groupId> 24 <artifactId>test-hd-api</artifactId> 25 <version>0.0.1-SNAPSHOT</version> 26 </dependency> 27 28 <dependency> 29 <groupId>com.hd</groupId> 30 <artifactId>test-hd-service</artifactId> 31 <version>0.0.1-SNAPSHOT</version> 32 </dependency> 33 34 <dependency> 35 <groupId>com.hd</groupId> 36 <artifactId>test-hd-resource</artifactId> 37 <version>0.0.1-SNAPSHOT</version> 38 </dependency> 39 40 <dependency> 41 <groupId>com.hd</groupId> 42 <artifactId>test-hd-foundation</artifactId> 43 <version>0.0.1-SNAPSHOT</version> 44 </dependency> 45 46 <!-- Servlet --> 47 <dependency> 48 <groupId>javax.servlet</groupId> 49