Override dependency in subprojects


(Mayura Viraj) #1

Hi all,

I’m writing a multi-project gradle build. In root i have defined dependency that will required by most sub-projects. ex:

subprojects {
    apply plugin: 'java'
    dependencies {
        compile fileTree(dir: "main_build_libs", includes: ['**/*.jar'])
    }
}

Now, in one subproject, i need to exclude those dependencies and add different libs I tried following in subproject build

project(':core:projectB') {
    dependencies {
        compile fileTree(dir: 'main_build_libs', exclude: ['**/*.jar'])
        compile fileTree(dir: 'specific_build_libs', include: ['**/*.jar'])
    }
}

This configuration does not work . I get compile failure due projectB referring to “main_build_libs” jars.

can anyone help me ?

Thanks in advance


(Peter Niederwieser) #2

It’s always better to direct configuration at the right places to begin with, rather than trying to compensate later. For example:

subprojects {
    apply plugin: 'java'
}
configure(subprojects - project(':core:projectB')) {
    dependencies {
        compile fileTree(dir: "main_build_libs", includes: ['**/*.jar'])
    }
}
project(':core:projectB') {
    dependencies {
        compile fileTree(dir: 'specific_build_libs', includes: ['**/*.jar'])
    }
}

(Mayura Viraj) #3

Thanks for the quick response. I tried your code. still projectB does not override main dependencies.


(Peter Niederwieser) #4

There is no longer any overriding here. Anyway, your original code (which I copied) has a typo: ‘include:’ should be ‘includes:’.


(Mayura Viraj) #5

Thanks for the reply, still i’m strangling to resolve my original issue. I fixed the typos and moved configuration to root build as suggested by you, I’m new to gradle and tried searching goole for lot of time to resolve this issue. This may be simple issue, but appreciate your support.


(Peter Niederwieser) #6

Given the information you provided, I can’t say more. Note that file paths are always relative to the project associated with the current build script. You may also want to check out the multi-project builds chapter in the Gradle User Guide.