maven 學習總結(五)——聚合與繼承

Coding-lover發表於2015-09-22

一、聚合

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

1.1、聚合配置程式碼

 <modules>
       <module>模組一</module>
       <module>模組二</module>
       <module>模組三</module>
 </modules>

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

 <modules>
       <module>../Hello</module>  
       <module>../HelloFriend</module>        
       <module>../MakeFriends</module>
 </modules>

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

二、繼承

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

2.1、繼承配置程式碼

 <parent>  
          <groupId>me.gacl.maven</groupId>
          <artifactId>ParentProject</artifactId>
          <version>0.0.1-SNAPSHOT</version>
          <relativePath>../ParentProject/pom.xml</relativePath>  
 </parent>

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

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

 <properties>
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     <junit.version>4.9</junit.version>
     <maven.version>0.0.1-SNAPSHOT</maven.version>
 </properties>

  訪問屬性的方式為${junit.version},例如:
  

 <dependency>
     <groupId>junit</groupId>
     <artifactId>junit</artifactId>
     <version>${junit.version}</version>
     <scope>test</scope>
 </dependency>  

2.3、父模組用dependencyManagement進行管理

<dependencyManagement>
    <dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit.version}</version>
        <scope>test</scope>
    </dependency>    
    <dependency>
            <groupId>cn.itcast.maven</groupId>
            <artifactId>HelloFriend</artifactId>
            <version>${maven.version}</version>
            <type>jar</type>
            <scope>compile</scope>
       </dependency>
     </dependencies>
</dependencyManagement>

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

三、聚合與繼承的關係

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

四、聚合與繼承實戰演練

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

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

  Parent專案是其它三個專案的父專案,主要是用來配置一些公共的配置,其它三個專案再通過繼承的方式擁有Parent專案中的配置,首先配置Parent專案的pom.xml,新增對專案的Hello、HelloFriend、MakeFriends這三個模組進行聚合以及jar包依賴,pom.xml的配置資訊如下:

  Parent專案的pom.xml配置

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>me.gacl.maven</groupId>
    <artifactId>Parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <name>Parent</name>
    <url>http://maven.apache.org</url>

    <!-- 對專案的Hello、HelloFriend、MakeFriends這三個模組進行聚合 -->
    <modules>
        <module>../Hello</module>
        <module>../HelloFriend</module>
        <module>../MakeFriends</module>
    </modules>

    <!-- 定義屬性 -->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <junit.version>4.9</junit.version>
        <maven.version>0.0.1-SNAPSHOT</maven.version>
    </properties>

    <!-- 用dependencyManagement進行jar包依賴管理 -->
    <dependencyManagement>
        <!-- 配置jar包依賴 -->
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <!-- 訪問junit.version屬性 -->
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>me.gacl.maven</groupId>
                <artifactId>Hello</artifactId>
                <!-- 訪問maven.version屬性 -->
                <version>${maven.version}</version>
                <scope>compile</scope>
            </dependency>
            <dependency>
                <groupId>me.gacl.maven</groupId>
                <artifactId>HelloFriend</artifactId>
                <!-- 訪問maven.version屬性 -->
                <version>${maven.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

  在Hello專案的pom.xml中繼承Parent專案的pom.xml配置

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>
  <artifactId>Hello</artifactId>

      <!-- 繼承Parent專案中的pom.xml配置 -->
       <parent>  
          <groupId>me.gacl.maven</groupId>
         <artifactId>Parent</artifactId>
          <version>0.0.1-SNAPSHOT</version>
          <!-- 使用相對路徑 -->
        <relativePath>../Parent/pom.xml</relativePath>  
    </parent>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
    </dependencies>
</project>

  在HelloFriend專案的pom.xml中繼承Parent專案的pom.xml配置

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <artifactId>HelloFriend</artifactId>
    <name>HelloFriend</name>

    <!-- 繼承Parent專案中的pom.xml配置 -->
    <parent>
        <groupId>me.gacl.maven</groupId>
        <artifactId>Parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../Parent/pom.xml</relativePath>
    </parent>
    <dependencies>
        <dependency>
            <!-- Parent專案的pom.xml檔案配置中已經指明瞭要使用的Junit的版本號,因此在這裡新增junit的依賴時,
            可以不指明<version></version>和<scope>test</scope>,會直接從Parent專案的pom.xml繼承 -->
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
        <!-- HelloFriend專案中使用到了Hello專案中的類,因此需要新增對Hello.jar的依賴 
        Hello.jar的<version>和<scope>也已經在Parent專案的pom.xml檔案配置中已經指明瞭
        因此這裡也可以省略不寫了
        -->
        <dependency>
            <groupId>me.gacl.maven</groupId>
            <artifactId>Hello</artifactId>
        </dependency>
    </dependencies>
</project>

  在MakeFriends專案的pom.xml中繼承Parent專案的pom.xml配置
  

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <artifactId>MakeFriends</artifactId>
    <!-- 繼承Parent專案中的pom.xml配置 -->
    <parent>
        <groupId>me.gacl.maven</groupId>
        <artifactId>Parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../Parent/pom.xml</relativePath>
    </parent>
    <dependencies>
        <dependency>
        <!-- Parent專案的pom.xml檔案配置中已經指明瞭要使用的Junit的版本號,因此在這裡新增junit的依賴時,
            可以不指明<version></version>和<scope>test</scope>,會直接從Parent專案的pom.xml繼承 -->
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
        <dependency>
        <!-- MakeFriends專案中使用到了HelloFriend專案中的類,因此需要新增對HelloFriend.jar的依賴 
        HelloFriend.jar的<version>和<scope>也已經在Parent專案的pom.xml檔案配置中已經指明瞭
        因此這裡也可以省略不寫了
        -->
            <groupId>me.gacl.maven</groupId>
            <artifactId>HelloFriend</artifactId>
        </dependency>
    </dependencies>
</project>

  以上的四個專案的pom.xml經過這樣的配置之後,就完成了在Parent專案中聚合Hello、HelloFriend、MakeFriends這三個子專案(子模組),而Hello、HelloFriend、MakeFriends這三個子專案(子模組)也繼承了Parent專案中的公共配置,這樣就可以使用Maven一次性構建所有的專案了,如下圖所示:

  選中Parent專案的pom.xml檔案→【Run As】→【Maven install】,這樣Maven就會一次性同時構建Parent、Hello、HelloFriend、MakeFriends這四個專案,如下圖所示:

轉載自:Maven學習總結(五)——聚合與繼承

相關文章