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

Posted

技术标签:

【中文标题】Gradle 5 JUnit BOM 和 Spring Boot 版本不正确【英文标题】:Gradle 5 JUnit BOM and Spring Boot Incorrect Versions 【发布时间】:2019-07-03 01:17:56 【问题描述】:

我正在使用 Gradle 5 的 BOM(材料清单)feature。这就是我对 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's BOM,我们看到所有org.junit.jupiter 依赖项都以5.4.0 版本列出(在项目中解析为5.1.1),所有org.junit.platform 依赖项都以1.4.0 版本列出,它们在项目。

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

编辑:

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

终于:

我决定直接使用Spring Boot Dependencies BOM 并移除 Gradle 插件:

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

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

我在下面接受了 Sam Brannen 的回答,因为他很好地解释了问题是如何发生的以及解决问题的方法,我认为这与使用旧版本 Gradle 的人相关。

【问题讨论】:

./gradlew dependencyInsight --dependency junit-jupiter-api --configuration testCompileClasspath 打印什么? @MarcPhilipp imgur.com/dGBFyxf 【参考方案1】:

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

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

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

这在 Spring Boot for Gradle 和 for Maven 中有记录。

对于 Spring Boot,JUnit Jupiter 版本的名称是“junit-jupiter.version”。您可以找到 Spring Boot 2.1.2 here 的所有托管版本的名称。

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

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

你可以看到我确实做到了here。

使用 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

【讨论】:

谢谢@Sam Brannen!我不得不说,虽然我不太高兴使用enforcedPlatform() 并没有达到你认为它应该达到的效果。我会采纳你的建议,谢谢!【参考方案2】:

JUnit 5.4.0 简化了它的工件,现在为 Jupiter 提供了一个工件 - org.junit:junit-jupiter。即,您也应该简化 Gradle 文件:

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

【讨论】:

太好了!我得到JUnit Jupiter 5.4.0 版,但我可以看到JUnit Jupiter API、Engine 和Params 的各个版本仍在解析为5.1.1 我替换了:``` sdfsdfs ```` testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2' testRuntimeOnly 'org.junit.jupiter:junit- jupiter-engine:5.8.2' testImplementation 'org.junit.jupiter:junit-jupiter-params:5.8.2' with:【参考方案3】:

确保将 JUnit 的 BOM放在其他也引用 JUnit 的 BOM 之前。第一个 BOM 获胜并锁定所有后续工件的版本。

有关使用 Maven 和 Spring Boot 的类似设置,请参阅此问题:https://github.com/sormuras/junit-platform-maven-plugin/issues/29#issuecomment-456958188

【讨论】:

谢谢!我将所有 BOM 移到了 dependencies 块的顶部,但仍然没有得到版本 5.4.0。然后我怀疑它可能来自我使用的 Gradle Spring Boot 插件,所以当我评论它时,我得到了版本5.4.0。如何禁用来自 Gradle Spring Boot 插件 plugins.gradle.org/plugin/org.springframework.boot 的 JUnit SO 对这个新/后续问题的回答是什么? ;) 也许在这里你可以得到更快的帮助:discuss.gradle.org

以上是关于Gradle 5 JUnit BOM 和 Spring Boot 版本不正确的主要内容,如果未能解决你的问题,请参考以下文章

Gradle 没有找到我使用 Kotlin 和 JUnit 5 进行的测试

Gradle 找不到集成测试、JUnit 5 和 Spring Boot:没有找到给定的测试包括:

如何将 JUnit 5 与 Gradle 一起使用?

如何在Gradle中使用JUnit 5?

Kotlin 1.5.10,Gradle 7.0.2_2 - 找不到方法 testCompile() group=org.junit.jupiter,name=junit-jupiter-api,ve

如何将 Gradle 中的原生 JUnit 5 支持与 Kotlin DSL 结合使用?