Gradle 管理传递依赖

Gradle manage transitive dependencies

我对 gradle 比较陌生,我有一个 (gradle) 项目,我想使用 junit 4.13 或更高版本(用于断言抛出)。您可以在此处找到该项目:https://gitlab.com/connorbutch/reading-comprehension/-/blob/master/reading-comprehension-server-quarkus-impl/build.gradle 问题是该项目的一个依赖项引入了 junit 4.12,因此它最终出现在我的传递依赖项的类路径中(我已经使用项目报告插件验证了这一点).我正在尝试使用以下方法从类路径中删除 junit 4.12,但无济于事(在每个方法旁边找到结果):

implementation('io.quarkus:quarkus-resteasy-jackson') { exclude group: 'junit', module: 'junit'}

constraints{ //force upgrade to junit 4.13 for compatibility (and gives us assertThrows) implementation("junit:junit") { because "4.13 has assertThrows" version { strictly "$junitVersion" } } }

尝试强制升级的错误日志: `无法解析配置的所有文件':reading-comprehension-server-quarkus-impl:testCompileClasspath'。

Could not resolve junit:junit:4.13. Required by: project :reading-comprehension-server-quarkus-impl > Cannot find a version of 'junit:junit' that satisfies the version constraints: Dependency path 'com.connor:reading-comprehension-server-quarkus-impl:1.0.0-SNAPSHOT' --> 'junit:junit:4.13' Constraint path 'com.connor:reading-comprehension-server-quarkus-impl:1.0.0-SNAPSHOT' --> 'junit:junit:{strictly 4.13}' because of the following reason: 4.13 has assertThrows Constraint path 'com.connor:reading-comprehension-server-quarkus-impl:1.0.0-SNAPSHOT' --> 'io.quarkus:quarkus-universe-bom:1.7.1.Final' --> 'junit:junit:4.12'

Could not resolve junit:junit:{strictly 4.13}. Required by: project :reading-comprehension-server-quarkus-impl > Cannot find a version of 'junit:junit' that satisfies the version constraints: Dependency path 'com.connor:reading-comprehension-server-quarkus-impl:1.0.0-SNAPSHOT' --> 'junit:junit:4.13' Constraint path 'com.connor:reading-comprehension-server-quarkus-impl:1.0.0-SNAPSHOT' --> 'junit:junit:{strictly 4.13}' because of the following reason: 4.13 has assertThrows Constraint path 'com.connor:reading-comprehension-server-quarkus-impl:1.0.0-SNAPSHOT' --> 'io.quarkus:quarkus-universe-bom:1.7.1.Final' --> 'junit:junit:4.12' `

谁能帮我强制我的项目使用 junit 4.13(或更高版本)?谢谢。

旁注: 我怀疑这可能是一个 Maven 本机项目的问题,因此传递依赖项可能存在问题(如在 Maven 中,没有 api 与实现范围)。

JUnit 4.12 而不是更高版本 (4.13) 的原因是因为这一行:https://gitlab.com/connorbutch/reading-comprehension/-/blob/master/reading-comprehension-server-quarkus-impl/build.gradle#L43

您正在强制执行此平台 (BOM) 的约束,该平台将 JUnit 4.12 定义为约束:https://search.maven.org/artifact/io.quarkus/quarkus-universe-bom/1.7.0.Final/pom

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
</dependency>

如果您只是想将该平台用作“推荐”,则应使用platform

implementation(platform("..."))

将来,您可以使用 dependencyInsight 任务来弄清楚为什么 Gradle 选择特定版本的依赖项:

./gradlew :reading-comprehension-server-quarkus-impl:dependencyInsight --dependency junit:junit --configuration testCompileClasspath

运行 以上给出:

> Task :reading-comprehension-server-quarkus-impl:dependencyInsight
junit:junit:4.13
   variant "compile" [
      org.gradle.status              = release (not requested)
      org.gradle.usage               = java-api
      org.gradle.libraryelements     = jar (compatible with: classes)
      org.gradle.category            = library

      Requested attributes not found in the selected variant:
         org.gradle.dependency.bundling = external
         org.gradle.jvm.version         = 11
   ]
   Selection reasons:
      - By constraint
      - By conflict resolution : between versions 4.13 and 4.12

junit:junit:4.13
\--- testCompileClasspath

junit:junit:4.12 -> 4.13
\--- io.quarkus:quarkus-universe-bom:1.7.0.Final
     \--- testCompileClasspath

A web-based, searchable dependency report is available by adding the --scan option.

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.3/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 2s
1 actionable task: 1 executed

这个问题的重要部分是:

Selection reasons:
          - By constraint
          - By conflict resolution : between versions 4.13 and 4.12

因为平台是强制的,版本有冲突,所以平台约束会胜出。

进一步阅读:https://docs.gradle.org/current/userguide/java_platform_plugin.html#sec:java_platform_consumption