Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transitive file dependencies in gradle

Tags:

java

gradle

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?

like image 552
Ingo Kegel Avatar asked Nov 28 '11 11:11

Ingo Kegel


1 Answers

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.

like image 121
Eric Wendelin Avatar answered Sep 19 '22 13:09

Eric Wendelin