Maven學習5-彙總與繼承

來源:互聯網
上載者:User

標籤:hdu   pll   msu   read   udp   cfs   nfa   dns   for   

一、彙總

  如果我們想一次構建多重專案模組,那我們就需要對多重專案模組進行彙總

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>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

Maven學習5-彙總與繼承

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.