我正在使用 Gradle 5 的 BOM( Material list )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()
中指定的那样。 block 。
我接受了 Sam Brannen 下面的回答,因为他很好地解释了问题是如何发生的以及如何解决它,我认为这对于那些使用旧版本 Gradle 的人来说是相关的。
最佳答案
How do I disable JUnit coming from the Gradle Spring Dependency Management plugin?
对于初学者来说,如果您使用 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
关于spring-boot - Gradle 5 JUnit BOM 和 Spring Boot 版本不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57823111/