Fallback Dependency

Hi,

is it somehow possible to define a fallback dependency in case the “original” dependency can’t be found?

It would be very helpful during our development process, where some libraries (commons) may exists with a slightly different artifactId (general artifact id + a branch name like commons-f1), but if not, just use the default commons.

regards
Guenther

You could somewhat emulate this behavior using a LenientConfiguration, which is essentially just a configuration that doesn’t throw an exception if it fails to resolve a dependency. Using the example below, a dependency assigned to the optional configuration will be placed first on the classpath if it happens to be found.

configurations {
    optional
}

sourceSets {
    main {
        compileClasspath = files({configurations.optional.resolvedConfiguration.lenientConfiguration.getFiles(Specs.satisfyAll())}).plus(compileClasspath)
    }
}

dependencies {
    optional "org.foo:some-dependency:1.0.0"
}
1 Like

Appreciate solution but I don’t want to risk double libraries on the classpath because we use spring, and I see myself in a dependency hell very soon, when the classpath is messed up.

The only alternative I see would be to trigger resolution of the lenient configuration during the configuration phase and conditionally include the other dependency based on the result. The downside of this is that doing dependency resolution early like this will impact project configuration time negatively.

Yes, I also think that resolving the dependency at configuration time is somehow needed. But his would take some programming effort in the gradle script, wouldn’t it?