Fallback Dependency


(Günther Grill) #1

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


(Mark Vieira) #2

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

(Günther Grill) #3

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.


(Mark Vieira) #4

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.


(Günther Grill) #5

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?