概述
几乎所有的基于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’)}}