Gradle 5 JUnit BOM 和 Spring Boot 版本不正确

2024-05-10

我正在使用 Gradle 5 的 BOM(物料清单)feature https://docs.gradle.org/5.0/userguide/managing_transitive_dependencies.html#sec:bom_import。这是我对 JUnit 5 依赖项的描述:

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()锁定指定版本)

检验JUnit 5 的 BOM http://central.maven.org/maven2/org/junit/junit-bom/5.4.0/junit-bom-5.4.0.pom我们看到所有org.junit.jupiter列出了依赖项和版本5.4.0(解决项目中的5.1.1)以及所有org.junit.platform列出了依赖项和版本1.4.0在项目中正确解决。

我不确定我错过了什么,希望在这里得到一些帮助。谢谢!

EDIT:

我使用了 Sormuras 响应并将所有 BOM 移到了顶部dependencies {}阻止但仍未获得版本5.4.0。然后我怀疑它可能来自Gradle Spring 依赖管理 https://docs.spring.io/spring-boot/docs/current/gradle-plugin/reference/html/#managing-dependencies我使用的插件,所以当我将其注释掉时,我得到了版本 JUnit 5.4.0。如何禁用来自 Gradle Spring 依赖管理插件的 JUnit?

FINALLY:

我决定使用Spring Boot 依赖项 BOM https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-dependencies直接删除 Gradle 插件:

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

我想该插件是为 Gradle 5 之前的 Gradle 版本创建的,其中您无法使用 BOM 文件。现在有了 BOM 支持,我可以直接包含它。这样我的 JUnit 版本就是我在enforcedPlatform() block.

我接受了 Sam Brannen 下面的回答,因为他很好地解释了问题是如何发生的以及如何解决它,我认为这对于那些使用旧版本 Gradle 的人来说是相关的。


如何禁用来自 Gradle Spring 依赖管理插件的 JUnit?

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

除此之外,每当您使用 Spring 的依赖管理插件并想要覆盖托管版本时,您都必须通过覆盖插件使用的 BOM 中定义的版本的确切名称来实现。

这在 Spring Boot 中有记录对于 Gradle https://docs.spring.io/spring-boot/docs/2.1.2.RELEASE/gradle-plugin/reference/html/#managing-dependencies-customizing and 对于 Maven https://docs.spring.io/spring-boot/docs/2.1.2.RELEASE/reference/htmlsingle/#using-boot-maven-parent-pom.

对于 Spring Boot,JUnit Jupiter 版本的名称是“junit-jupiter.version”。您可以找到 Spring Boot 2.1.2 所有托管版本的名称here https://github.com/spring-projects/spring-boot/blob/v2.1.2.RELEASE/spring-boot-project/spring-boot-dependencies/pom.xml.

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

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

你可以看到我已经做到了here https://github.com/sbrannen/spring-events/blob/d803b0fe2e007f2264973d135c4b5ac814f49477/build.gradle#L38.

使用 Maven,您可以按如下方式覆盖它。

<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 https://docs.spring.io/platform/docs/current/reference/html/getting-started-overriding-versions.html

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Gradle 5 JUnit BOM 和 Spring Boot 版本不正确 的相关文章

随机推荐