maven 中parent 與 dependencyManagement

今天我們來說說maven的pom.xml文件中parent,dependencyManagement 標籤。

首先我們來說說parent標籤,其實這個不難解釋,就是父的意思,pom也有繼承的。比方說我現在有A,B,C,A是B,C的父級。現在就是有一個情況B,C其實有很多jar都是共同的,其實是可以放在父項目裏面,這樣,讓B,C都繼承A就方便管理了。

子模塊的寫法如下:

 

<parent>  
        <groupId>com.lala</groupId>  
        <artifactId>my-parent</artifactId>  
        <version>0.0.1</version>  
    </parent>  

 

 

 

現在說說dependencyManagement 標籤,其實這個也很好理解,比方說你的A項目pom文件內容如下

 

<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>com.lala</groupId>  
    <artifactId>my-parent</artifactId>  
    <version>0.0.1</version>  
    <packaging>pom</packaging>  
  
    <properties>  
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
    </properties>  
  
    <dependencies>  
        <dependency>  
            <groupId>junit</groupId>  
            <artifactId>junit</artifactId>  
            <version>4.11</version>  
            <scope>test</scope>  
        </dependency>  
        <dependency>  
            <groupId>redis.clients</groupId>  
            <artifactId>jedis</artifactId>  
            <version>2.4.0</version>  
            <scope>provided</scope>  
        </dependency>  
    </dependencies>  
  
    <dependencyManagement>  
        <dependencies>  
            <dependency>  
                <groupId>javax.mail</groupId>  
                <artifactId>javax.mail-api</artifactId>  
                <version>1.5.3</version>  
                <scope>provided</scope>  
            </dependency>  
        </dependencies>  
    </dependencyManagement>  
  
    <build>  
        <plugins>  
            <plugin>  
                <groupId>org.apache.maven.plugins</groupId>  
                <artifactId>maven-compiler-plugin</artifactId>  
                <version>3.3</version>  
                <configuration>  
                    <source>1.8</source>  
                    <target>1.8</target>  
                    <verbose>true</verbose>  
                </configuration>  
            </plugin>  
        </plugins>  
    </build>  
</project>  

在dependencyManagement標籤包圍的jar裏面,B,C是沒有繼承的,也就是不會自動引入的,你要 在子項目中顯示的聲明一些依賴纔可以引入父項目

的jar(不需要指定version,scope).parent標籤是將父項目的依賴,子項目會自動引入的,這也是兩者的區別。

就是說如果B,C現在想引用A中的jar了,在dependencyManagement裏面的jar,你直接在B,C中寫<parent>標籤是沒有用的,不會自動引入的,

如果你想引用上訴父類A中的javax.mail-api架包,還要重寫一下<groupId> ,<artifactId>(<version>標籤可以不寫)。

有些人會說這樣做有什麼意義?爲什麼不直接全部引用呢?這裏就可能有這麼一個例子,比方說B,C中需要大部分A中的jar,但有有個別jar B,C

是不需要的,而且B,C不需要jar可能還不一樣,這時候就需要dependencyManagement標籤去選擇了

還有一點要說明的,就是不一定要父子關係才能用到dependencyManagement的,這點很重要。比如我在B項目,不引用任何父項目,我在B中引用dependencyManagement

標籤也是可以的,如下

//只是對版本號進行管理,不會實際引入jar  
<dependencyManagement>  
      <dependencies>  
            <dependency>  
                <groupId>org.springframework</groupId> //jar 包身份限定  
                <artifactId>spring-core</artifactId>  
                <version>3.2.7</version>  //版本號的聲明
            </dependency>  
    </dependencies>  
</dependencyManagement>  
  
//會實際下載jar包  
<dependencies>  
       <dependency>  
                <groupId>org.springframework</groupId>  
                <artifactId>spring-core</artifactId> //不聲明version 標籤,則會繼承
       </dependency>  
</dependencies>

 

總結:

pom文件中,jar的版本判斷有2種途徑

1,第一種就是最常見的depenency中寫version

2,如果不是第一種,那麼maven會到自己的pom文件或者父pom中找有沒有對該artifactid和groupid進行版本申明的,如果有就拿那個版本號,沒有就報錯

3,如果兩種都寫錯,那麼第一種覆蓋第二種

 

 

 

 

 

發佈了60 篇原創文章 · 獲贊 66 · 訪問量 16萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章