How do I create a non-transitive or compile-only dependency?


(rds6235) #1

I’m working on setting up a project that will serve as a helper library for configuring several other different and unrelated third party libraries. That means this project will have compile-time dependencies on all those third party libraries, but I don’t want all of them to be dragged along as transitive dependencies when someone includes my project. Instead, it should depend on the appropriate libraries already being present on the classpath. What’s the best way to accomplish this in Gradle? I’m fine using a non-release build if there’s something in progress that will help.


(rds6235) #2

I’ve come up with the following, which appears to work. I added an “optional” configuration, which I manually add to the compile and runtime classpaths but not the transitive dependencies of the project.

configurations {

optional

}

sourceSets {

main {

compileClasspath += configurations.optional

runtimeClasspath += configurations.optional

}

test {

compileClasspath += configurations.optional

runtimeClasspath += configurations.optional

}

}

dependencies {

compile(

)

optional(

)

testCompile(

)

}

and for IDEA support:

idea {

module {

scopes.PROVIDED.plus += configurations.optional

}

}

I think this is doing what I want, but it seems like too much repetition. Is there a better way to get the “optional” dependencies applied to the appropriate class paths?


(Peter Niederwieser) #3

“optional” is not a built-in concept, so it will take a bit of configuration. The ‘sourceSets’ part can be abbreviated to:

sourceSets.all*.compileClasspath += configurations.optional
sourceSets.all*.runtimeClasspath += configurations.optional

I’d also put the code into its own build script so that it can be easily reused.