英文:
How to get a spring boot dependency version in Gradle?
问题
以下是翻译后的内容:
"Having plugin disabled" 翻译为 "禁用插件后"
"```groovy
plugins {
id 'org.springframework.boot' version '2.7.9' apply false
}
"And imported BOM manually, I want to get version in `dependencies`" 翻译为 "手动导入 BOM 后,我想在 `dependencies` 中获取版本"
"```groovy
subprojects {
dependencyManagement {
imports {
mavenBom SpringBootPlugin.BOM_COORDINATES
}
}
dependencies {
//here I need version
implementation "org.slf4j:slf4j-api${???}"
}
}
```" 不需要翻译,保持原样。
"It's known [from the documentation](https://docs.spring.io/spring-boot/docs/3.0.4/reference/htmlsingle/#appendix.dependency-versions.properties):" 翻译为 "这在[文档](https://docs.spring.io/spring-boot/docs/3.0.4/reference/htmlsingle/#appendix.dependency-versions.properties)中已知:"
"> The spring-boot-dependencies bom that is automatically imported when the dependency management plugin is applied uses properties to control the versions of the dependencies that it manages. Browse the Dependency versions Appendix in the Spring Boot reference for a complete list of these properties." 翻译为 "> 当应用依赖管理插件时,自动导入的 spring-boot-dependencies BOM 使用属性来控制它管理的依赖项的版本。请浏览 Spring Boot 参考文档中的“Dependency versions”附录以获取这些属性的完整列表。"
"> To customize a managed version you set its corresponding property. For example, to customize the version of SLF4J which is controlled by the `slf4j.version` property:
ext['slf4j.version'] = '1.7.20'
```" 翻译为 "> 要自定义已管理版本,您需要设置相应的属性。例如,要自定义由 slf4j.version
属性控制的 SLF4J 版本:"
"But it's not know how to **get** one" 翻译为 "但不知道如何 **获取** 一个版本"
<details>
<summary>英文:</summary>
Having plugin disabled
```groovy
plugins {
id 'org.springframework.boot' version '2.7.9' apply false
}
And imported BOM manually, I want to get version in dependencies
subprojects {
dependencyManagement {
imports {
mavenBom SpringBootPlugin.BOM_COORDINATES
}
}
dependencies {
//here I need version
implementation "org.slf4j:slf4j-api${???}"
}
}
It's known from the documentation:
> The spring-boot-dependencies bom that is automatically imported when the dependency management plugin is applied uses properties to control the versions of the dependencies that it manages. Browse the Dependency versions Appendix in the Spring Boot reference for a complete list of these properties.
> To customize a managed version you set its corresponding property. For example, to customize the version of SLF4J which is controlled by the slf4j.version
property:
ext['slf4j.version'] = '1.7.20'
But it's not know how to get one
答案1
得分: 1
像这样:
dependencies {
implementation "org.slf4j:slf4j-api:${dependencyManagement.managedVersions["org.slf4j:slf4j-api"]}"
}
英文:
Like this:
dependencies {
implementation "org.slf4j:slf4j-api:${dependencyManagement.managedVersions["org.slf4j:slf4j-api"]}"
}
答案2
得分: 0
我知道我的回答与问题略有关联,但对于那些寻找io.spring.dependency-management
用法的人,请注意 - 这个插件的工作方式与Maven的<dependencyManagement>
不同。
它可以降级传递性运行时依赖项,并导致难以检测的运行时错误。在我的情况下,jakarta.json:jakarta.json-api
的传递性运行时依赖项从2.x
降级为1.x
。
更鼓励的方法,顺便说一句,与<dependencyManagement>
完全相同的方式是使用Gradle的platform
依赖,如下所示:
dependencies {
implementation platform('org.springframework.boot:spring-boot-dependencies:<Boot版本在这里>')
// 这里不需要指定版本!
implementation 'org.slf4j:slf4j-api';
}
这种方法的唯一缺点是,如果使用org.springframework.boot
Gradle插件,您将需要两次指定Spring Boot版本 - 在plugins
块和在dependencies
中。
英文:
I know my answer is slightly related to the question but for those who looking for io.spring.dependency-management
usages be aware - the way this plugin works is not the same as Maven's <dependencyManagement>
It can downgrade transitive runtime dependencies and cause hard to detect runtime bugs. In my case the transitive runtime dependency on jakarta.json:jakarta.json-api
was downgraded from 2.x
to 1.x
.
The more encouraged approach, which by the way works exactly the same as <dependencyManagement>
is using Gradle platform
dependency like so:
dependencies {
implementation platform('org.springframework.boot:spring-boot-dependencies:<Boot version here>')
// no need to specify versions here!
implementation 'org.slf4j:slf4j-api'
}
The only downside of this approach is that you will need to specify Spring Boot version twice if you using org.springframework.boot
Gradle plugin - in plugins
block and in dependencies
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论