Combine resolutionStrategy with exclusion

2.3k views Asked by At

I am using Gradle 6.5.1. I have added a custom resolutionStrategy to a build.gradle file - but now an exclusion like this is not being applied:

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 !!
}

So it seems that custom resolutionStrategies and exclusions are not composable, at least not by default, in Gradle 6.5.1. Is there some way I can make Gradle fall back to its "default" resolutionStrategy if mine is not relevant? If not, what should I do?

1

There are 1 answers

0
RenatoIvancic On

Issue

You have to have some special resolutionStrategy in place in order to overwrite the exclusion for cats-core_2.12.

Or

Dependency on cats-core_2.12 is being resolved as transitive dependency from other dependency and not com.example:foo-bar_2.12 as you expect. You should use gradle dependency command and post here the result of where cats-core is being resolved.

Example

I have following simple build.gradle build script with similar exclusion rule and resolutionStrategy as you can see below and cats-core will still be excluded from dependencies as expected

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'
    }
}

Dependencies:

Running following command ./gradlew dependencies

You can see that cats-core is not listed in dependencies as it's excluded.

...
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)
...

Alternative

If exclusion in your case is not forking for specific dependency, maybe exclusion for all configurations might help like example below:

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