如果你有一个包含行implementation 'org.springframework.boot:spring-boot-starter-web'
的build.gradle文件,你如何选择它下载的jar的版本你得到最新的吗?我见过一个项目,它是 2.2.4 版本,但在另一个项目中,我看到了 2.2.5 版本的相同行。
最佳答案
由于您删除了 Spring Boot 这个名称,我假设该项目已生成 Spring Initializr 。使用 Initializr 生成的项目应用了两个插件:
-
org.springframework.boot
(Reference Documentation) -
io.spring.dependency-management
(Reference Documentation)
io.spring.dependency-management
是 Spring 为 Gradle 构建提供类似 Maven 的依赖管理的固定方式。它允许声明一次依赖版本,然后在声明实际依赖时省略该版本。
org.springframework.boot
插件执行以下操作:
When you apply the
io.spring.dependency-management
plugin, Spring Boot’s plugin will automatically import thespring-boot-dependencies
bom from the version of Spring Boot that you are using. This provides a similar dependency management experience to the one that’s enjoyed by Maven users. For example, it allows you to omit version numbers when declaring dependencies that are managed in the bom. To make use of this functionality, simply declare dependencies in the usual way but omit the version number.(From: Managing Dependencies)
这在实践中意味着什么?
当您为 Spring Boot 2.1.14 生成项目时,您的 build.gradle
将如下所示:
plugins {
id 'org.springframework.boot' version '2.1.14.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
}
org.springframework.boot
插件指示 io.spring.dependency-management
应用 Spring Boot 2.1.14 的 Material list (BOM) 。 BOM 声明了 spring-boot-starter-web
的以下版本:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.14.RELEASE</version>
</dependency>
(来自:Maven Central)
这种组合允许在 build.gradle
中声明对 spring-boot-starter-web
的依赖,而无需提供实际版本:
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
}
如果您要更改 org.springframework.boot
Gradle 插件的版本,则会应用与 Spring Boot 版本匹配的不同版本。
您可能会问,为什么要付出如此巨大的努力?
We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss.
这就是原因。
关于java - 如何在 build.gradle 中选择版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62207686/