本文介绍了Gradle 5 JUnit BOM和Spring Boot不正确的版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Gradle 5的BOM(物料清单)功能.这就是我对JUnit 5依赖项的描述方式:

I am using Gradle 5's BOM (Bill of Materials) feature. This is how I describe it for my JUnit 5 dependencies:

testImplementation(enforcedPlatform("org.junit:junit-bom:5.4.0")) // JUnit 5 BOM
testImplementation("org.junit.jupiter:junit-jupiter-api")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
testImplementation("org.junit.jupiter:junit-jupiter-params")

我的假设是提供BOM将把依赖关系的版本解析为5.4.0.但是,它们解析为5.1.1.我不知道为什么. (我还要求enforcedPlatform()锁定指定的版本)

My assumption is that providing the BOM will resolve the versions of the dependencies to 5.4.0. However, they get resolved to 5.1.1. I am not sure why. (I also request enforcedPlatform() to lock the specified version)

检查 JUnit 5的BOM表,我们看到所有org.junit.jupiter依赖项都列出了版本5.4.0(在项目中解析为5.1.1),所有org.junit.platform依赖项都列出了版本1.4.0,它们在项目中可以正确解析

Inspecting JUnit 5's BOM we see that all org.junit.jupiter dependencies are listed with version 5.4.0 (resolving to 5.1.1 in the project) and all org.junit.platform dependencies are listed with version 1.4.0 which resolve correctly in the project.

我不确定自己缺少什么,希望能在这里得到一些帮助.谢谢!

I am not sure what I am missing and was hoping to get some help here. Thanks!

我使用了Sormuras响应并将所有BOM都移到了dependencies {}块的顶部,但是仍然没有获得版本5.4.0.然后,我怀疑它可能来自 Gradle Spring Dependency Management 插件,所以当我注释掉它时,得到的版本是JUnit 5.4.0.如何禁用来自Gradle Spring Dependency Management插件的JUnit?

I used Sormuras response and moved all BOMs at the top of the dependencies {} block but was still not getting version 5.4.0. Then I suspected it might be coming from the Gradle Spring Dependency Management plugin that I use, so when I commented it out, I got version JUnit 5.4.0. How do I disable JUnit coming from the Gradle Spring Dependency Management plugin?

最后一次:

我决定直接使用 Spring Boot依赖项BOM 并删除Gradle插件:

I decided to use the Spring Boot Dependencies BOM directly and remove the Gradle plugin:

implementation(platform("org.springframework.boot:spring-boot-dependencies:2.0.5.RELEASE"))

我想象该插件是为Gradle 5之前的那些版本的Gradle创建的,在该版本中您无法使用BOM表文件.现在有了BOM支持,我可以直接包括它.这样,我的JUnit版本就是在enforcedPlatform()块中指定的版本.

I imagine the plugin was created for those version of Gradle before Gradle 5 where you couldn't use a BOM file. Now with the BOM support I can directly include it. This way my version of JUnit is as I have specified it in the enforcedPlatform() block.

我接受了萨姆·布兰嫩(Sam Brannen)的以下回答,因为他很好地解释了问题的发生方式以及解决的方法,并且我认为这与使用较旧版本的Gradle的用户有关.

I accepted Sam Brannen's answer below because he explains well how the issue occurs and what solves it and I think it's relevant for those who use older versions of Gradle.

推荐答案

对于初学者来说,如果您使用的是Spring的依赖关系管理插件,则不应导入junit-bom,因为这会导致对这些依赖关系的重复管理(并可能会产生冲突).

For starters, if you are using the dependency management plugin from Spring, you should not be importing the junit-bom since that results in duplicate (and potentially conflicting) management of those dependencies.

除此之外,每当您使用Spring的依赖项管理插件并要覆盖托管版本时,都必须通过覆盖插件使用的BOM中定义的版本的确切名称来做到这一点.

Aside from that, whenever you use the dependency management plugin from Spring and want to override a managed version, you have to do it by overriding the exact name of the version defined in the BOM used by the plugin.

这在Spring Boot 用于Gradle 对于Maven .

This is documented in Spring Boot for Gradle and for Maven.

对于Spring Boot,JUnit Jupiter版本的名称为"junit-jupiter.version".您可以找到Spring Boot 2.1.2的所有托管版本的名称此处.

For Spring Boot the name of the JUnit Jupiter version is "junit-jupiter.version". You can find the names of all managed versions for Spring Boot 2.1.2 here.

因此,在Gradle中,您可以按如下方式覆盖它.

So, in Gradle you would override it as follows.

ext['junit-jupiter.version'] = '5.4.0'.

您可以看到我已经完全完成了此处.

You can see that I have done exactly that here.

使用Maven时,您可以按如下所示进行覆盖.

With Maven you would override it as follows.

<properties>
    <junit-jupiter.version>5.4.0</junit-jupiter.version>
</properties>

此处的其他背景信息: https://docs.spring.io/platform/docs/current/reference/html/getting-started-overriding-versions.html

Further background information here: https://docs.spring.io/platform/docs/current/reference/html/getting-started-overriding-versions.html

这篇关于Gradle 5 JUnit BOM和Spring Boot不正确的版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 19:46