在開發2個以上模塊的時候,每個模塊都是一個 Maven Project。比如搜索平台,學習平台,考試平台。開發的時候可以自己管自己獨立編譯,測試,運行。但如果想要將他們整合起來,我們就需要一個聚合工程。
(1) 父模塊的創建.
父模塊一般承擔聚合模塊和統一管理依賴的作用,沒有實際代碼和資源文件.
父模塊就是創建一個普通的 Maven Project , 此處省略.
(2) 子模塊的創建
① 子模塊需要創建為 Maven Module 項目.
② 選擇該子模塊所屬的父模塊
打包方式 : web 項目需要打 war 包,其他的比如 dao 層, service 層, entity 層都可以打 jar 包.

實際的目錄結構:子模塊其實是包含在父模塊文件夾里面的.
(4) 父模塊的 pom 文件.
<modules> <module>PYG-pojo</module> <module>PYG-dao</module> <module>PYG-Commons</module> ... </modules>
(5) 子模塊的 pom 文件
指明它的父模塊是誰
<modelVersion>4.0.0</modelVersion> <!-- 指定它的父模塊是誰 --> <parent> <groupId>it.com.pyg</groupId> <artifactId>PYG-Parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <!-- 當前子模塊的名字 --> <artifactId>PYG-dao</artifactId>
(6) 聚合工程的依賴傳遞
一般都是在父模塊的 pom 中定義項目用到的依賴以及版本,
然后在子模塊的 pom 中, 需要什么依賴就直接引入, 不引入版本號, 依賴會自動從父模塊中傳遞到子模塊中.
① 父模塊中定義依賴
<!-- 統一定義版本號 -->
<properties> <spring.version>4.3.7.RELEASE</spring.version> </properties>
<!-- 父模塊統一管理依賴 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
② 子模塊中使用依賴
子模塊中使用的話, 不需要定義版本號.
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
</dependencies>
(7) 聚合工程的安裝.
聚合工程不需要每個模塊分別安裝, 只要對父工程安裝即可.
① 在 打包方式為 pom 的模塊中, 添加插件.
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8080</port>
<path>/</path>
</configuration>
</plugin>
② 在父模塊上, 右鍵 -> Run As -> Maven install
③ 執行結果 : 父工程和子模塊都Build成功了
(7) 多模塊的 WEB 項目運行.
在 打包方式為 pom 的模塊上, 右鍵
maven clean tomcat7:run
