Take the 2-minute tour ×

I would like to control which of my dependencies in a multi-project Java build are transitive. My current solution is to set up an "export" configuration in the root project:

allprojects {
    configurations {
        export {
            description = 'Exported classpath'
        compile {
            extendsFrom export

Project A has multiple file dependencies:

dependencies {
    compile files('A.jar', 'B.jar')
    export files('C.jar')

Project B has a dependency on project A, but only C.jar should be on the classpath for compilation, so add:

dependencies {
    export project(path: ':A', configuration:'export')

This produces the desired results, A.jar and B.jar are not on the class path, but C.jar is on the classpath for compilation.

I am unsure if this is "gradle" way of doing things. To configure transitivity, I would rather specify an attribute or a configuration closure to the dependency entries in project A, instead of using a different "export" configuration.

Is this possible for file dependencies, or is there another way to achieve this?

share|improve this question

1 Answer 1

If I understand your scenario correctly, then yes it's easy to do this. Just add an options closure to the end of the dependency declaration to prevent transitive dependencies (I've changed A,B,C .jar to X,Y,Z because I'm guessing they don't coincide with projects A and B):

// Project A build.gradle
dependencies {
   compile(files('X.jar', 'Y.jar')) { transitive = false }
   export files('Z.jar')

Which would prevent X.jar and Y.jar from being added to the classpath for project B.

Alternatively, and I don't know how well this would work for you and don't really recommend it (just want you to know of the possibilities) you could do this in project B's build.gradle:

configurations.compile.dependencies.find { it.name == "A.jar" }.exclude(jar: it)
configurations.compile.dependencies.find { it.name == "B.jar" }.exclude(jar: it) 

Hope that helps.

share|improve this answer
I had already tried that and it did not work. Now I digged a little and it turns out that the transitive property is only available for module dependencies, but not for file collection dependencies. So you you could say that this is a missing feature as of 1.0-milestone 6. Maybe you can change the first part of your answer to reflect this, then I can accept it. –  Ingo Kegel Dec 2 '11 at 10:32
I'll do you one better. I'm talking to the founder of Gradle in about 5 hours and I'll ask him about this quick. –  Eric Wendelin Dec 2 '11 at 13:11
Awsome, thanks! –  Ingo Kegel Dec 2 '11 at 14:00
@EricWendelin So what did you find out? –  SK9 Aug 24 '13 at 5:43

Your Answer


