maven中dependencyManagement與dependencies
一:maven中dependencyManagement與dependencies應用場景(繼承)
1:dependencyManagement使用
當我們的專案模組很多的時候,我們使用Maven管理專案非常方便,幫助我們管理構建、文件、報告、依賴、scms、釋出、分發的方法。可以方便的編譯程式碼、進行依賴管理、管理二進位制庫等等。
由於我們的模組很多,所以我們又抽象了一層,抽出一個itoo-base-parent來管理子專案的公共的依賴。為了專案的正確執行,必須讓所有的子專案使用依賴項的統一版本,必須確保應用的各個專案的依賴項和版本一致,才能保證測試的和釋出的是相同的結果。
在我們專案頂層的POM檔案中,我們會看到dependencyManagement元素。通過它元素來管理jar包的版本,讓子專案中引用一個依賴而不用顯示的列出版本號。Maven會沿著父子層次向上走,直到找到一個擁有dependencyManagement元素的專案,然後它就會使用在這個dependencyManagement元素中指定的版本號。
Itoo-base-parent(pom.xml) :
<dependencyManagement> <dependencies> <dependency> <groupId>org.eclipse.persistence</groupId> <artifactId>org.eclipse.persistence.jpa</artifactId> <version>${org.eclipse.persistence.jpa.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>${javaee-api.version}</version> </dependency> </dependencies> </dependencyManagement>
Itoo-base(pom.xml)
<!--繼承父類--> <parent> <artifactId>itoo-base-parent</artifactId> <groupId>com.tgb</groupId> <version>0.0.1-SNAPSHOT</version> <relativePath>../itoo-base-parent/pom.xml</relativePath> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>itoo-base</artifactId> <packaging>ejb</packaging> <!--依賴關係--> <dependencies> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> </dependency> <dependency> <groupId>org.eclipse.persistence</groupId> <artifactId>org.eclipse.persistence.jpa</artifactId> <scope>provided</scope> </dependency> </dependencies> </project>
這樣做的好處:統一管理專案的版本號,確保應用的各個專案的依賴和版本一致,才能保證測試的和釋出的是相同的成果,因此,在頂層pom中定義共同的依賴關係。同時可以避免在每個使用的子專案中都宣告一個版本號,這樣想升級或者切換到另一個版本時,只需要在父類容器裡更新,不需要任何一個子專案的修改;如果某個子專案需要另外一個版本號時,只需要在dependencies中宣告一個版本號即可。子類就會使用子類宣告的版本號,不繼承於父類版本號。
2:dependencie使用
相對於dependencyManagement,所有生命在dependencies裡的依賴都會自動引入,並預設被所有的子專案繼承。
3:區別
dependencies即使在子專案中不寫該依賴項,那麼子專案仍然會從父專案中繼承該依賴項(全部繼承)
dependencyManagement**裡只是宣告依賴,並不實現引入,因此子專案需要顯示的宣告需要用的依賴。如果不在子專案中宣告依賴,是不會從父專案中繼承下來的;只有在子專案中寫了該依賴項,並且沒有指定具體版本,才會從父專案中繼承該項,並且version和scope都讀取自父pom;另外如果子專案中指定了版本號,那麼會使用子專案中指定的jar版本
二:dependencyManagement中的type和scope(引用)
maven當中避免重複發明輪子的方法,一種是繼承(上面第一種方式就是繼承),另一種是引用。 maven中配置引 用關係的方法是,
父pom檔案裡面的
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
子pom檔案
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
</dependencies>
子 pom只需到父pom的
點選圖片中這個地方會跳出該pom檔案中所有的依賴
(圖一)
(圖二)
如圖二中的openfeign依賴,就是圖一中點擊出來的,圖一中有所有spring-cloud的元件依賴,所以只需在子pom檔案中定義好依賴,就會自動找到父pom檔案中的版本。同理下面圖3是spring-cloud Alibaba的相關的pom依賴,也只需要我們在子pom檔案中定義好,就能找到父pom裡面的檔案。
(圖3)
同理:對於pom檔案的parent,點選spring-boot-starter-parent,跳出如圖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/>
</parent>
可以看到這裡也是
那麼我們在springboot依賴時候,也不用在指定版本了,只要是springboot有的依賴,都可以不用寫版本號,會自動找到父pom檔案,然後根據父pom找到相關的元件的依賴。
所以這些能夠不寫版本號的依賴,最終原因是,你在子pom檔案引入的元件,一定要在父pom檔案中的dependencies裡面這個元件確實真的要存在,這個是根本。不論是springcloud,亦或者是springcloud Alibaba 等,都只是這個原理。
例如springboot的:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
......
</dependencies>
參考連結:
https://www.cnblogs.com/feibazhf/p/7886617.html
https://cloud.tencent.com/developer/article/1479488
https://blog.csdn.net/onedaycbfly/article/details/78927488