将解决策略与排除相结合

问题描述

我使用的是 Gradle 6.5.1。我已将自定义分辨率策略添加build.gradle 文件 - 但现在没有应用这样的排除:

testImplementation("com.example:foo-bar_2.12:$dependencies_version"){
    exclude group: 'org.scala-lang',module: 'scala-library'
    exclude group: 'org.typelevel',module: 'cats-core_2.12' // <- !! NOT WORKING !!
}

因此,在 Gradle 6.5.1 中,自定义解析策略和排除似乎不可组合,至少认情况下不可组合。如果我的不相关,有什么方法可以让 Gradle 回退到它的“认”分辨率策略?如果没有,我该怎么办?

解决方法

问题

您必须有一些特殊的解决方案策略才能覆盖 cats-core_2.12 的排除。

对cats-core_2.12 的依赖被解析为来自其他依赖的传递依赖,而不是如您所料的com.example:foo-bar_2.12。您应该使用 gradle dependency 命令并在此处发布解决 cat-core 问题的结果。

示例

我有以下简单的 build.gradle 构建脚本,其具有类似的排除规则和分辨率策略,如下所示,并且cats-core 仍会按预期从依赖项中排除

dependencies {

    testImplementation('com.github.julien-truffaut:monocle-core_2.13:3.0.0-M5'){
        exclude group: 'org.scala-lang',module: 'scala-library'
        exclude group: 'org.typelevel',module: 'cats-core_2.13' // <- WORKS
    }

    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
}

configurations.all {
    resolutionStrategy {
        failOnVersionConflict()

        preferProjectModules()

        force 'org.typelevel:cats-core_2.13:2.4.0'

        // cache dynamic versions for 10 minutes
        cacheDynamicVersionsFor 10*60,'seconds'
        // don't cache changing modules at all
        cacheChangingModulesFor 0,'seconds'
    }
}

依赖:

运行以下命令 ./gradlew dependencies

您可以看到,cats-core 没有在依赖项中列出,因为它被排除在外。

...
testRuntimeClasspath - Runtime classpath of source set 'test'.
+--- com.github.julien-truffaut:monocle-core_2.13:3.0.0-M5
|    \--- org.typelevel:cats-free_2.13:2.6.0
|         \--- org.typelevel:simulacrum-scalafix-annotations_2.13:0.5.4
+--- org.junit.jupiter:junit-jupiter-api:5.7.0
|    +--- org.junit:junit-bom:5.7.0
|    |    +--- org.junit.jupiter:junit-jupiter-api:5.7.0 (c)
...

替代方案

如果在您的情况下排除不是针对特定依赖项,那么排除所有配置可能会有所帮助,如下例所示:

configurations {
    all.collect { configuration ->
        configuration.exclude group: 'org.typelevel',module: 'cats-core_2.13'
    }
}