若是咱們想一次構建多個項目模塊,那咱們就須要對多個項目模塊進行聚合apache
1 <modules> 2 <module>模塊一</module> 3 <module>模塊二</module> 4 <module>模塊三</module> 5 </modules>
例如:對項目的Hello、HelloFriend、MakeFriends這三個模塊進行聚合maven
1 <modules> 2 <module>../Hello</module> 3 <module>../HelloFriend</module> 4 <module>../MakeFriends</module> 5 </modules>
其中module的路徑爲相對路徑。ui
繼承爲了消除重複,咱們把不少相同的配置提取出來,例如:grouptId,version等url
1 <parent> 2 <groupId>me.gacl.maven</groupId> 3 <artifactId>ParentProject</artifactId> 4 <version>0.0.1-SNAPSHOT</version> 5 <relativePath>../ParentProject/pom.xml</relativePath> 6 </parent>
繼承代碼過程當中,能夠定義屬性,例如:spa
1 <properties> 2 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 3 <junit.version>4.9</junit.version> 4 <maven.version>0.0.1-SNAPSHOT</maven.version> 5 </properties>
訪問屬性的方式爲${junit.version},例如:xml
1 <dependency> 2 <groupId>junit</groupId> 3 <artifactId>junit</artifactId> 4 <version>${junit.version}</version> 5 <scope>test</scope> 6 </dependency>
1 <dependencyManagement> 2 <dependencies> 3 <dependency> 4 <groupId>junit</groupId> 5 <artifactId>junit</artifactId> 6 <version>${junit.version}</version> 7 <scope>test</scope> 8 </dependency> 9 <dependency> 10 <groupId>cn.itcast.maven</groupId> 11 <artifactId>HelloFriend</artifactId> 12 <version>${maven.version}</version> 13 <type>jar</type> 14 <scope>compile</scope> 15 </dependency> 16 </dependencies> 17 </dependencyManagement>
這樣的好處是子模塊能夠有選擇行的繼承,而不須要所有繼承。繼承
聚合主要爲了快速構建項目,繼承主要爲了消除重複ci
建立四個Maven項目,以下圖所示:it
這四個項目放在同一個目錄下,方便後面進行聚合和繼承io
Parent項目是其它三個項目的父項目,主要是用來配置一些公共的配置,其它三個項目再經過繼承的方式擁有Parent項目中的配置,首先配置Parent項目的pom.xml,添加對項目的Hello、HelloFriend、MakeFriends這三個模塊進行聚合以及jar包依賴,pom.xml的配置信息以下:
Parent項目的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 5 <groupId>me.gacl.maven</groupId> 6 <artifactId>Parent</artifactId> 7 <version>0.0.1-SNAPSHOT</version> 8 <packaging>pom</packaging> 9 10 <name>Parent</name> 11 <url>http://maven.apache.org</url> 12 13 <!-- 對項目的Hello、HelloFriend、MakeFriends這三個模塊進行聚合 --> 14 <modules> 15 <module>../Hello</module> 16 <module>../HelloFriend</module> 17 <module>../MakeFriends</module> 18 </modules> 19 20 <!-- 定義屬性 --> 21 <properties> 22 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 23 <junit.version>4.9</junit.version> 24 <maven.version>0.0.1-SNAPSHOT</maven.version> 25 </properties> 26 27 <!-- 用dependencyManagement進行jar包依賴管理 --> 28 <dependencyManagement> 29 <!-- 配置jar包依賴 --> 30 <dependencies> 31 <dependency> 32 <groupId>junit</groupId> 33 <artifactId>junit</artifactId> 34 <!-- 訪問junit.version屬性 --> 35 <version>${junit.version}</version> 36 <scope>test</scope> 37 </dependency> 38 <dependency> 39 <groupId>me.gacl.maven</groupId> 40 <artifactId>Hello</artifactId> 41 <!-- 訪問maven.version屬性 --> 42 <version>${maven.version}</version> 43 <scope>compile</scope> 44 </dependency> 45 <dependency> 46 <groupId>me.gacl.maven</groupId> 47 <artifactId>HelloFriend</artifactId> 48 <!-- 訪問maven.version屬性 --> 49 <version>${maven.version}</version> 50 </dependency> 51 </dependencies> 52 </dependencyManagement> 53 </project>
在Hello項目的pom.xml中繼承Parent項目的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 4 <modelVersion>4.0.0</modelVersion> 5 <artifactId>Hello</artifactId> 6 7 <!-- 繼承Parent項目中的pom.xml配置 --> 8 <parent> 9 <groupId>me.gacl.maven</groupId> 10 <artifactId>Parent</artifactId> 11 <version>0.0.1-SNAPSHOT</version> 12 <!-- 使用相對路徑 --> 13 <relativePath>../Parent/pom.xml</relativePath> 14 </parent> 15 16 <dependencies> 17 <dependency> 18 <groupId>junit</groupId> 19 <artifactId>junit</artifactId> 20 </dependency> 21 </dependencies> 22 </project>
在HelloFriend項目的pom.xml中繼承Parent項目的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 <artifactId>HelloFriend</artifactId> 5 <name>HelloFriend</name> 6 7 <!-- 繼承Parent項目中的pom.xml配置 --> 8 <parent> 9 <groupId>me.gacl.maven</groupId> 10 <artifactId>Parent</artifactId> 11 <version>0.0.1-SNAPSHOT</version> 12 <relativePath>../Parent/pom.xml</relativePath> 13 </parent> 14 <dependencies> 15 <dependency> 16 <!-- Parent項目的pom.xml文件配置中已經指明瞭要使用的Junit的版本號,所以在這裏添加junit的依賴時, 17 能夠不指明<version></version>和<scope>test</scope>,會直接從Parent項目的pom.xml繼承 --> 18 <groupId>junit</groupId> 19 <artifactId>junit</artifactId> 20 </dependency> 21 <!-- HelloFriend項目中使用到了Hello項目中的類,所以須要添加對Hello.jar的依賴 22 Hello.jar的<version>和<scope>也已經在Parent項目的pom.xml文件配置中已經指明瞭 23 所以這裏也能夠省略不寫了 24 --> 25 <dependency> 26 <groupId>me.gacl.maven</groupId> 27 <artifactId>Hello</artifactId> 28 </dependency> 29 </dependencies> 30 </project>
在MakeFriends項目的pom.xml中繼承Parent項目的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 <artifactId>MakeFriends</artifactId> 5 <!-- 繼承Parent項目中的pom.xml配置 --> 6 <parent> 7 <groupId>me.gacl.maven</groupId> 8 <artifactId>Parent</artifactId> 9 <version>0.0.1-SNAPSHOT</version> 10 <relativePath>../Parent/pom.xml</relativePath> 11 </parent> 12 <dependencies> 13 <dependency> 14 <!-- Parent項目的pom.xml文件配置中已經指明瞭要使用的Junit的版本號,所以在這裏添加junit的依賴時, 15 能夠不指明<version></version>和<scope>test</scope>,會直接從Parent項目的pom.xml繼承 --> 16 <groupId>junit</groupId> 17 <artifactId>junit</artifactId> 18 </dependency> 19 <dependency> 20 <!-- MakeFriends項目中使用到了HelloFriend項目中的類,所以須要添加對HelloFriend.jar的依賴 21 HelloFriend.jar的<version>和<scope>也已經在Parent項目的pom.xml文件配置中已經指明瞭 22 所以這裏也能夠省略不寫了 23 --> 24 <groupId>me.gacl.maven</groupId> 25 <artifactId>HelloFriend</artifactId> 26 </dependency> 27 </dependencies> 28 </project>
以上的四個項目的pom.xml通過這樣的配置以後,就完成了在Parent項目中聚合Hello、HelloFriend、MakeFriends這三個子項目(子模塊),而Hello、HelloFriend、MakeFriends這三個子項目(子模塊)也繼承了Parent項目中的公共配置,這樣就可使用Maven一次性構建全部的項目了,以下圖所示:
選中Parent項目的pom.xml文件→【Run As】→【Maven install】,這樣Maven就會一次性同時構建Parent、Hello、HelloFriend、MakeFriends這四個項目,以下圖所示: