Maven的dependencyManagement和傳遞依賴

當使用了傳遞依賴,也就是使用了沒有顯示聲明的依賴時,若是繼承的<dependencyManagement/>中聲明瞭使用的傳遞依賴的版本,那麼最終使用的依賴是<dependencyManagement/>中聲明的版本。html

Spring舉一個栗子:spring

1.mvn dependency:tree

先看下項目依賴的基礎組件使用的Spring版本。apache


能夠看到基礎組件使用的Spring版本爲3.1.3,不能lambda表達式(被坑過)。maven


2.mvn dependency:analyze

接下來分析下依賴,關注Spring的相關依賴。ide


能夠看到Spring做爲傳遞依賴使用,版本是基礎組件使用的版本3.1.3ui


3.parent module

而後在項目的parent module<dependencyManagement/>加入一個spring-framework-bom來管理Spring的版本。this


${springframework.version}5.0.4.RELEASEspa


4.從新執行mvn dependency:tree


能夠看到Spring的版本變爲了5.0.4.RELEASE3d


在執行mvn dependency:analyze後看到使用的Spring版本也變成了5.0.4.RELEASEcode


5.文檔

  • Dependency management - this allows project authors to directly specify the versions of artifacts to be used when they are encountered in transitive dependencies or in dependencies where no version has been specified. In the example in the preceding section a dependency was directly added to A even though it is not directly used by A. Instead, A can include D as a dependency in its dependencyManagement section and directly control which version of D is used when, or if, it is ever referenced.


文檔鏈接: maven.apache.org/guides/intr…
相關文章
相關標籤/搜索