Maven的繼承與聚合

maven繼承管理 讓版本的管理只在一個地方改變

 modules用於聚合,把執行的項目都放到同一的地方用module包括,可以省去一個個項目去mvn install,這樣可以所有項目一次聚合 mvn install

傳遞性依賴原則:

A-->B
A-->C

1.路徑最近者優先
2.路徑相同,第一聲明者優先

注意:
1.dependencyManagement中定義的依賴子module不會共享
2.dependencies中定義的依賴子module可以共享

dependencyManagement的使用 就是方便管理版本,子項目中要引入group id和atifact id

一、聚合

  如果我們想一次構建多個項目模塊,那我們就需要對多個項目模塊進行聚合

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>
複製代碼

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

dependencies 和 dependencyManagement 的區別在於:

前者,即使在子項目中不寫該依賴項,那麼子項目仍然會從父項目中繼承該依賴項。

後者,如果在子項目中不寫該依賴項,那麼子項目中是不會從父項目繼承該依賴項的;只有在子項目中寫了該依賴項,纔會從父項目中繼承該項,並且version 和 scope 都讀取自 父pom。

三、聚合與繼承的關係

  聚合主要爲了快速構建項目,繼承主要爲了消除重複

四、聚合與繼承實戰演練

  創建四個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    <!--定義了依賴的關係,讓其他項目繼承這些包,不用寫版本,同一控制了-->
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       <!--不用寫版本  parent那裏同一控制了-->
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這四個項目,如下圖所示:

  

轉載自:http://www.cnblogs.com/xdp-gacl/p/4058008.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章