What is the recommended way to specify optional dependencies?


(Dimitar Dimitrov) #1

I have a projects which provides a number of adaptors to third-party libraries. Each adaptor is small enough, so putting it in its own projects would be an overkill. Clients of the library would typially use one or few adaptors, so I would like to make the third party dependencies optional. That is, when client projects depend on the adaptors project, the regular dependencies will be added transitively, while the ‘optional’ dependencies on third party libs will be ignored.

I assume that I can do this by poking with the ‘default’ configuration, but I am not sure when is the best hook (i.e. can I do it in the dependency section, or do I need to wait until the deps are resolved?

An example will be very appreciated - i.e. take this as a starting point:

project.name = "My Fancy Loggers"
dependencies {
   compile project("myCoreUtils")
         // this one should always be added transitively
.
   compile "log4j:log4j:1.23",
            // these should be optional and
            "jakarta:commons-logging:1.23",
// not included in the 'default' config
           "logback:logback:0.45:6"
 }

(Martin Lemanski) #2

I think it should go with runtime or providedCompile. But I can’t distinguish where those configurations come from. Gradle is still a mystery for me.

runtime “log4j:log4j:1.23”

So, if you have a dependency on fancyLoggers, you will have to provide those jar files…


(René Groeschke) #3

Hello, well runtime dependencies and optional dependencies are something different. Gradle does not have a built-in concept of “optional” dependencies. One way to go, would be to adapt the dependencies section of the pom directly

[installer, deployer]*.pom*.whenConfigured {pom ->
    pom.dependencies.find {dep -> dep.groupId == 'group3' && dep.artifactId == 'runtime' }.optional = true
}