Spring+Gradle依赖管理学习笔记

  • spring boot 1.x的gradle插件自动会引入dependency-management,但是在spring boot 2.x中不会自动引入,需要手动引入,类似于:

plugins {
    id "org.springframework.boot" version "2.1.4.RELEASE"
    id "io.spring.dependency-management" version "1.0.6.RELEASE"
}
  • Spring boot gradle插件引入dependency-management会做自动做一件核心事情:spring boot grale插件会自动调用dependency-management引入org.springframework.boot:spring-boot-dependencies:SPRING_BOOT_VERSION,其中SPRING_BOOT_VERSION也是spring boot gradle插件的版本,spring-boot-dependencies中维护了spring boot生态下的bom数据。具体流程请参考SpringBootPlugin.java和DependencyManagementPluginAction.java文件。

  • Gradle 5.x开始自动提供bom功能,因此可以不用引入spring 的dependency management插件,而直接用:

dependencies {
    // import a BOM
    implementation platform('org.springframework.boot:spring-boot-dependencies:1.5.8.RELEASE')

    // define dependencies without versions
    implementation 'com.google.code.gson:gson'
    implementation 'dom4j:dom4j'
}
  • 对于spring cloud来说,spring boot插件并不像自动引入spring-boot-dependencies那样自动引入spring cloud的bom,因此需要我们自己显式地引入spring cloud相关的bom,比如对于spring cloud sleuth来说:
plugins {
    id "org.springframework.boot" version "2.1.4.RELEASE"
    id "io.spring.dependency-management" version "1.0.6.RELEASE"
}

dependencyManagement {
     imports {
          mavenBom "org.springframework.cloud:spring-cloud-sleuth:${springCloudSleuthVersion}"
     }
}
dependencies {
    compile 'org.springframework.cloud:spring-cloud-starter-sleuth'
}

你可能感兴趣的:(Spring+Gradle依赖管理学习笔记)