如何在Gradle中获取Spring Boot依赖的版本?

huangapple go评论118阅读模式
英文:

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 &#39;org.springframework.boot&#39; version &#39;2.7.9&#39; 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 &quot;org.slf4j:slf4j-api${???}&quot;
  }
}

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[&#39;slf4j.version&#39;] = &#39;1.7.20&#39;

But it's not know how to get one

答案1

得分: 1

像这样:

dependencies {
  implementation "org.slf4j:slf4j-api:${dependencyManagement.managedVersions["org.slf4j:slf4j-api"]}"
}

在SB项目中的示例

英文:

Like this:

dependencies {
  implementation &quot;org.slf4j:slf4j-api:${dependencyManagement.managedVersions[&quot;org.slf4j:slf4j-api&quot;]}&quot;
}

Referent example in SB project

答案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 &lt;dependencyManagement&gt;

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 &lt;dependencyManagement&gt; is using Gradle platform dependency like so:

dependencies {
    implementation platform(&#39;org.springframework.boot:spring-boot-dependencies:&lt;Boot version here&gt;&#39;)

    // no need to specify versions here!
    implementation &#39;org.slf4j:slf4j-api&#39;

}

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.

huangapple
  • 本文由 发表于 2023年3月7日 18:19:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75660674.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定