概述
幾乎所有的基於JVM的軟件項目都需要依賴外部類庫來重用現有的功能。自動化的依賴管理可以明確依賴的版本,可以解決因傳遞性依賴帶來的版本衝突。
工件座標:group、name、version
常用倉庫:
- mavenLocal mavenCentral(http://search.maven.org/) jcenter
- 自定義maven倉庫(私服)
- 文件倉庫
依賴的傳遞性:B依賴A,如果C依賴B,那麼C依賴A。
依賴階段配置:
compile、runtime
testCompile、testRuntime
依賴階段關係:
compile<----extends-------runtime
/|\ /|\
| |
testComile<---extends----testRuntime
解決衝突:
- 查看依賴報告
修改默認結局策略:
configurations.all{ resolutionStrategy{failOnVersionConflict()}} - 排除傳遞性依賴
compile(‘org.hibernate:hibernate-core:3.6.3.Final’){exclude group:“org.slf4j”,module:“slf4j-api”} - 強制一個版本
configurations.all{ resolutionStrategy{force’org.slf4j:slf4j-api:1.7.24’)}}