failOnVersionConflict only for certain dependencies

(amp) #1

Is it possible to enforce failOnVersionConflict() not for the entire configuration, but for only certain group dependency(by name or by group).

(Ben Yoon) #2

Running into this issue as well. Any ideas?

(Louis Jacomet) #3

There is no built-in support for that, but with an afterResolve hook on a configuration, it is possible to achieve it in a custom way:

plugins {
    `java-library`
}

repositories {
    jcenter()
}

dependencies {
    // Creating conflct on animal-sniffer
    implementation("com.google.guava:guava:27.0.1-jre")
    implementation("org.codehaus.mojo:animal-sniffer-annotations:1.16")

    // Creating conflict on hamcrest
    testImplementation("org.hamcrest:hamcrest-core:1.2")
    testImplementation("junit:junit:4.12")
}

configurations {
    testCompileClasspath.get().incoming.afterResolve {
        resolutionResult.allComponents {
            if (selectionReason.isConflictResolution && moduleVersion != null) {
                if (moduleVersion!!.name.contains("hamcrest")) {
                    throw AssertionError("Cannot have a conflict on hamcrest")
                }
            }
        }
    }
}