1. 程式人生 > >Maven學習總結(七):Maven的聚合和繼承

Maven學習總結(七):Maven的聚合和繼承

一、聚合

  如果我們想一次構建多個專案模組,那我們就需要對多個專案模組進行聚合

1.1、聚合配置程式碼

1 <modules>
2       <module>模組一</module> 3 <module>模組二</module> 4 <module>模組三</module> 5 </modules>

  例如:對專案的Hello、HelloFriend、MakeFriends這三個模組進行聚合

1 <modules>
2       <module>../Hello</module> 3 <module>../HelloFriend</module> 4 <module>../MakeFriends</module> 5 </modules>

  其中module的路徑為相對路徑。

二、繼承

  繼承為了消除重複,我們把很多相同的配置提取出來,例如:grouptId,version等

2.1、繼承配置程式碼

複製程式碼
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>
複製程式碼

2.2、繼承程式碼中定義屬性

  繼承程式碼過程中,可以定義屬性,例如:

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},例如:

複製程式碼
1 <dependency>
2     <groupId>junit</groupId> 3 <artifactId>junit</artifactId> 4 <version>${junit.version}</version> 5 <scope>test</scope> 6 </dependency> 
複製程式碼

2.3、父模組用dependencyManagement進行管理

複製程式碼
 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>
複製程式碼

  這樣的好處是子模組可以有選擇行的繼承,而不需要全部繼承。

三、聚合與繼承的關係

  聚合主要為了快速構建專案,繼承主要為了消除重複

四、聚合與繼承實戰演練

  建立四個Maven專案,如下圖所示:

   

  這四個專案放在同一個目錄下,方便後面進行聚合和繼承

  

  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