Remove transitive dependency with classifier and replace with new one without classifier

Many of our internal libraries using dependency with classifier, something like
’com.x:artifact:1.0.0:classifier’ and now we get rid of the classifier in the new version.
But now i need to force gradle to ignore all the transitive dependencies and add just
’com.x:artifact:2.0.0’

I’m able to remove the dependency globally by

configurations {
all*.exclude group: “com.x”, module: “artifact”
}

But then I can’t add it back. So I need to remove just transition, not direct dependencies.

I tried resolutionStrategy with force and it also doesn’t work.

I tried to use also this:

configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        if (details.requested.name == 'artifact') {
            details.useTarget group: details.requested.group, name: details.requested.name, version: "3.0.0"                details.because "We want to use version 3.0.0 without classfier"
        }
    }
}

And it also doesn’t help, it looking for the new version, but still with classifier.

configurations.all {
    exclude module: "artifact"
    resolutionStrategy.force "com.x:artifact:3.0.0"
}

also doesn’t help, exclude is still more strong than force and dependency is completely missing.

Really no way how to replace one dependency by the same one just without classifier?

Hi Tomas, I found this way you can manipulate the classifier - in my case I wanted to add it ("linux-x86_64"), not to remove it:

configurations.all {
    resolutionStrategy.eachDependency {
        if (this.requested.module.toString() == "io.netty:netty-transport-native-epoll") {
            @Suppress("UnstableApiUsage")
            this.artifactSelection {
                @Suppress("UnstableApiUsage")
                this.selectArtifact(DependencyArtifact.DEFAULT_TYPE, null, "linux-x86_64")
            }
        }
    }
}

I’m using Kotlin DSL. Should work with Gradle 6.6 and onward.

Posting here for future reference

This works, thanks! In my case I needed to remove the classifier, so the syntax ended up like:

configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        if (details.requested.module.toString() == "commons-beanutils:commons-beanutils") {
            details.artifactSelection{
                it.selectArtifact(DependencyArtifact.DEFAULT_TYPE, null, null)
            }
        }
    }
}