dependencyManagement與dependencies區別
dependencyManagement里只是宣告依賴,并不實作引入,因此子專案需要顯式的宣告需要用的依賴,如果不在子專案中宣告依賴,是不會從父專案中繼承下來的;
生效方式: 只有在子專案中寫了該依賴項,并且沒有指定具體版本,才會從父專案中繼承該項 ,并且version和scope都讀取自父pom;另外如果子專案中指定了版本號,那么會使用子專案中指定的jar版本,
dependencies即使在子模塊中不寫該依賴項,那么子模塊仍然會從父專案中繼承該依賴項(全部繼承),
總結:
pom引入包進行良好的代碼規劃 會使得專案更容易維護,舉例說明
在cloud專案中,子專案都會用到spring-cloud-starter-bootstrap啟動器,這就可以寫到dependencies里,父層有,子一定會有,
還有些不一定所有專案都用,但是需要統一版本的com.github.pagehelper插件,就可以在父級dependencyManagement宣告,如果子有參考該插件,則可以不填寫版本號,統一會用父級的,
一、劃定版本 配置
<properties> <spring-cloud.version>2020.0.4</spring-cloud.version> </properties>
二、宣告依賴 此標簽內的只是宣告,子并不會有參考
<dependencyManagement> <dependencies> <!-- SpringCloud 微服務 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
三、參考依賴 此標簽參考 子專案也一定會有
<dependencies> <!-- bootstrap 啟動器 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bootstrap</artifactId> </dependency> </dependencies>
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/464979.html
標籤:Java
