04-maven學習-pom.xml解析

pom.xml裏面各個配置的含義以下:apache

<!-- 主項目標識,表示當前maven屬於哪一個實際項目,與包是同樣的 -->
     <groupId>反寫的公司網址+項目名</groupId>
     <!-- 模塊標識-->
     <artifactId>項目名+模塊名</artifactId>
     <!-- 
        版本號,通常由三個數字組成
        第一個0,表示大版本號,
        第二個0表示分支版本號,
        第三個0表示小版本號,
        snapshot 快照
        beta 公測
        alpha 內測
        Release 穩定
        GA正式發佈
      -->
     <version></version>
     <!-- 打包的方式,默認爲jar,還能夠打包成war,zip,pom -->
     <packaging></packaging>
  
     <!-- 項目描述名 -->
     <name></name>
     <!-- 項目的地址 -->
     <url></url>
     <!-- 項目的描述 -->
     <description></description>
     <!-- 開發人員信息 -->
     <developers></developers>
     <!-- 許可證信息 -->
     <licenses></licenses>
     <!-- 組織信息 -->
     <organization></organization>
  
    <!--  依賴列表-->
    <dependencies>
        <!-- 依賴項 -->
        <dependency>
            <groupId></groupId>
            <artifactId></artifactId>
            <version></version>
            <type></type>
            <!-- 依賴的範圍 -->
            <scope></scope>
            <!-- 設置依賴是否可選,默認false。 -->
            <optional></optional>
            <!-- 排除依賴傳遞列表 -->
            <exclusions>
                <exclusion>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <!-- 依賴的管理 -->
    <dependencyManagement>
         <dependencies>
             <dependency></dependency>
         </dependencies>
    </dependencyManagement>


    <build>
        <!-- 插件列表 -->
        <plugins>
            <plugin>
              <groupId></groupId>
              <artifactId></artifactId>
              <version></version>
            </plugin>
        </plugins>
    </build>
 
    <!-- 用於子模塊對父模塊pom的繼承 -->
    <parent></parent>
    <!-- 用來聚合多個maven項 -->
    <modules>
        <module></module>
    </modules>

例如上一節建立的以下:maven

  <groupId>org.maven.mavenDemo</groupId>
  <artifactId>mavenDemo01</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  
  <name>mavenDemo01</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

 

依賴範圍:<scop>

三種classpath:ide

  • 編譯
  • 測試
  • 運行

scop選項:測試

  • compile:默認的範圍,編譯測試運行都有效
  • provided:在編譯和測試時候有效
  • runtime:在測試和運行時有效
  • test:只在測試範圍有效
  • system:與本機系統相關聯,可移植性差。
  • import:導入的範圍,只使用在dependenceManagement中。表示從其餘的pom中導入dependecy的配置

 

依賴傳遞

這裏創建三個maven項目演示ui

demo02要依賴demo01,要想依賴,必須在本地倉庫安裝demo01的項目url

首先對demo01進行以下操做:spa

1,右鍵demo01,使用maven方式運行,將其打包:插件

2,將mavendemo01安裝到本地倉庫中,一樣執行maven方式運行,執行install命令。code

 

 

在demo02中加入demo01的依賴:xml

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    
 <dependency> <groupId>org.maven.mavenDemo</groupId> <artifactId>mavenDemo01</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
    
  </dependencies>

以下:

一樣方式將demo02安裝,爲了省去操做,同時執行兩個命令:clean  install

 

在demo03中依賴demo02項目。

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    
 <dependency> <groupId>org.maven.mavenDemo</groupId> <artifactId>mavenDemo02</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
  </dependencies>

一樣對demo03安裝到本地倉庫。

 

觀察mavenDemo03的依賴包發現,demo03原本只想依賴demo02,可是連demo01也依賴了,

這代表依賴具備傳遞性

要想不依賴demo01,使用exclusions,能夠排除demo01的依賴。

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    
    <dependency>
          <groupId>org.maven.mavenDemo</groupId>
          <artifactId>mavenDemo02</artifactId>
          <version>0.0.1-SNAPSHOT</version>
 <exclusions> <exclusion><!--下面填入mavenDemo01的座標--> <groupId>org.maven.mavenDemo</groupId> <artifactId>mavenDemo01</artifactId> </exclusion> </exclusions>
    </dependency>
  </dependencies>

保存後發現只剩demo02的依賴了。

 

 

 

依賴衝突

解決依賴衝突的兩條原則:

1,短路優先:A->B-C->X(jar)

       A->D->X(jar)

因爲第二條路比較短,會依賴第二條的方式。

 

2,先聲明先優先:

若是路徑長度相同,則誰先聲明,先解析誰。

 

 

 聚合和繼承

聚合

 若是想要將多個項目進行install,安裝到本地倉庫中,必須對其依次進行install命令。

而聚合能夠將多個項目放到一塊兒運行,同時安裝。

例如:將前面的三個項目聚合,一塊兒安裝。

新建一個mavenDemo04,在pom.xml裏面配置以下:

首先把packaging改爲pom

  <groupId>org.maven.mavenDemo</groupId>
  <artifactId>mavenDemo04</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>

而後添加聚合標籤:分別加入要安裝的三個項目

    <modules>
        <module> ../mavenDemo01 </module>
        <module> ../mavenDemo02 </module>
        <module>
            ../mavenDemo03
        </module>
    </modules>

而這裏的dependency無所謂了,能夠刪除。

而後運行maven命令,執行 clean  install命令。

此時就同時安裝了三個項目到本地倉庫。

 

 繼承

例如對於以前的三個項目中,每一個項目都依賴了一個junit,其實這樣重複了不少,可使用繼承方式代替這種。

1,新建一個demo5,demo5中定義以下:

  <groupId>org.maven.mavenDemo</groupId>
  <artifactId>mavenDemo05</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>

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

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <junit.version>3.8.1</junit.version>
  </properties>
  

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

一共關注三點:

1,將packaging改成pom

2,在properties中新增一共junit.version屬性。而後能夠在version標籤中經過${屬性名}的方式使用。

3,新增一個dependencyManagement標籤,將dependencyies標籤放進去。

 

假如在demo3中繼承這裏。

demo3中junit依賴定義以下:

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
 <version>3.8.1</version> <scope>test</scope>
    </dependency>
</dependencies>

修改,須要將上面的紅色部分刪除,而後添加一個parent標籤。parent標籤引入demo05的座標。

 <parent> <groupId>org.maven.mavenDemo</groupId> <artifactId>mavenDemo05</artifactId> <version>0.0.1-SNAPSHOT</version> </parent>
    
  <dependencies>
 <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency>
    
    <dependency>
          <groupId>org.maven.mavenDemo</groupId>
          <artifactId>mavenDemo02</artifactId>
          <version>0.0.1-SNAPSHOT</version>
          <exclusions>
              <exclusion>
                  <groupId>org.maven.mavenDemo</groupId>
                  <artifactId>mavenDemo01</artifactId>
              </exclusion>
          </exclusions>
    </dependency>
  </dependencies>
相關文章
相關標籤/搜索