How to exclude dependencies of other subproject in Gradle build?

11,812

Solution 1

You could exclude all transitive dependencies of a dependency:

compile('groupId:artifactId:version') {
    transitive = false
}

Or you could, but I do certainly not recommend this exclude every dependency by hand like this:

compile('groupId:artifactId:version') {
    exclude module: 'groupId:artifactId:version'
    ...
}

Solution 2

You can exclude a module from dependency

dependencies { 
    implementation (project(":sub_project")) {
        exclude group: 'grp1', module: 'mdl1'
        exclude group: 'grp2'
        exclude module: 'mdl2'
    }
}
Share:
11,812
Audrius Meškauskas
Author by

Audrius Meškauskas

A C++ developer in robotics now, ROS. Used to be Java developer for a long time.

Updated on June 28, 2022

Comments

  • Audrius Meškauskas
    Audrius Meškauskas almost 2 years

    I am developing a sub-project that is a part of the huge multi-project Gradle build. I need some Java classes from these other sub-projects, but I do not need any dependencies they drag in because some libraries I use require different versions of these.

    I initially configured the needed subprojects as

    compile project(':other_needed_sub_project')
    

    however in this case gradle eclipse task adds projects to the project tree. They dependencies appear on the class path, causing my application to pick the wrong versions. In Eclipse, included projects seem having priority over specified libraries.

    As a working solution, I currently build jars of the required sub-projects with existing Maven build and then use

    compile files('../other_needed_sub_project/target/dist/other_needed_sub_project.jar')
    

    that is exactly that I need - no libs attached. However this means that I must run a different build before building my subproject with Gradle.

    Could Gradle build the needed sub-projects and then only add references to they final jars in my Eclipse configuration?

  • vach
    vach over 8 years
    but this does not work with compile project(..) how is this an answer?
  • Andrew Westberg - BCSH
    Andrew Westberg - BCSH over 8 years
    @vach You need an extra set of parenthesis for: compile (project (':other_needed_subproject')) { transitive = false }