前言
-
网上有很多介绍SpringBoot和SpringCloud版本对应的文章,本文只是从实操的角度阐述,内容没有深度.
-
Maven中有dependencyManagement来声明依赖,Gradle中也有相应的功能.
Talk is cheap. Show me the code
build.gradle
plugins {
id "io.spring.dependency-management" version "1.0.8.RELEASE"
}
dependencyManagement {
imports {
mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Finchley.SR1'
}
}
dependencies {
compile "org.springframework.cloud:spring-cloud-starter-netflix-eureka-client"
}
此时观看gradle依赖,发现自动引入的springboot版本为2.0.4.RELEASE
,可以确定springboot的大版本了,同样的方式引入springboot的gradle插件org.springframework.boot
,并添加指定父依赖"org.springframework.boot:spring-boot-starter-parent:2.0.4.RELEASE"
最终效果如下:
plugins {
id "io.spring.dependency-management" version "1.0.8.RELEASE"
id "org.springframework.boot" version "2.0.4.RELEASE"
}
dependencyManagement {
imports {
// 插件org.springframework.boot已经包含了版本管理功能,这个声明可以不要
// mavenBom "org.springframework.boot:spring-boot-starter-parent:2.0.4.RELEASE"
mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Finchley.SR1'
}
}
dependencies {
testCompile "org.springframework.boot:spring-boot-starter-test"
compile "org.springframework.cloud:spring-cloud-starter-netflix-eureka-client"
}
思路
先根据SpringCloud版本管理插件确定SpringBoot版本,再根据这个大版本管理其他依赖.
问题
本地使用正常,但是上到环境后经常报无法下载,发现是新版gradle插件引入方式导致的,它默认从gradle官网拉取,而官网不稳定.
解决办法:
在项目根目录下的settings.gradle中指定插件仓库
pluginManagement {
resolutionStrategy {
}
repositories {
maven { url "http://maven.aliyun.com/nexus/content/groups/public" }
gradlePluginPortal()
}
}
Tips
如果使用了插件id "org.springframework.boot" version "2.0.4.RELEASE"
,那么会自动将springboot大版本设置为2.0.4.RELEASE
,dependencyManagement
任务中的mavenBom "org.springframework.boot:spring-boot-starter-parent:${springBootVersion}"
就可以不要了.
参考
https://plugins.gradle.org/plugin/io.spring.dependency-management
https://stackoverflow.com/questions/38221227/gradle-configuration-of-pluginrepository