【问题标题】:Gradle manage transitive dependenciesGradle 管理传递依赖
【发布时间】:2021-01-04 22:11:04
【问题描述】:

我对 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,但无济于事(找到每个旁边的结果):

  • 为引入 junit.12 的依赖项添加排除项 -> 这会导致 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'。

无法解析 junit:junit:4.13。 要求: 项目:阅读-理解-服务器-quarkus-impl > 找不到满足版本约束的“junit:junit”版本: 依赖路径 'com.connor:reading-comprehension-server-quarkus-impl:1.0.0-SNAPSHOT' --> 'junit:junit:4.13' 约束路径 'com.connor:reading-comprehension-server-quarkus-impl:1.0.0-SNAPSHOT' --> 'junit:junit:{strictly 4.13}' 因为以下原因:4.13 有 assertThrows 约束路径 '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:junit:{严格 4.13}。 要求: 项目:阅读-理解-服务器-quarkus-impl > 找不到满足版本约束的“junit:junit”版本: 依赖路径 'com.connor:reading-comprehension-server-quarkus-impl:1.0.0-SNAPSHOT' --> 'junit:junit:4.13' 约束路径 'com.connor:reading-comprehension-server-quarkus-impl:1.0.0-SNAPSHOT' --> 'junit:junit:{strictly 4.13}' 因为以下原因:4.13 有 assertThrows 约束路径 '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 vs 实现范围)。

【问题讨论】:

    标签: maven gradle dependency-management


    【解决方案1】:

    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

    【讨论】:

    • 哇,非常彻底(和有见地)的回答弗朗西斯科。作为 gradle 的(相对)新手,我非常感谢您的帮助。 (赞成并接受)
    猜你喜欢
    • 2016-11-30
    • 2019-04-07
    • 1970-01-01
    • 2013-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-23
    相关资源
    最近更新 更多