Is it possible in a root project to override/replace a sub project dependency?


(Nicolas Milliard) #1

I have a root project with two sub projects (Project A and Project B).

My subproject B has a dependency declared like

dependencies {
    compile 'group:name:1.0'
}

I would like in the rooproject override the dependency ‘group:name:1.0’ with a local jar file like

project(':project_b') {
    dependencies {
        compile files('lib/name.jar')
    }
}

But I don’t know how to achieve that.

Why: My subproject B is a git submodule. I cannot change it’s build.gradle easily.

Thanks for the Gradle project ^^


(René Groeschke) #2

Hello Nicolas,

to achieve this I would recommend the following: - declare a flat repo for project b in your rootproject - declare a custom resolution strategy for resolving “group:name:1.0” for b

project(':project_b') {
    repositories{
        flatDir('lib')
    }
    dependencies {
        compile files('lib/name.jar')
    }
    configurations.all {
        resolutionStrategy {
            force 'group:name:replacedVersion'
            // alternatively replace a specific lib explicitly:
            eachDependency { DependencyResolveDetails details ->
            if (details.requested.group == 'group' && details.requested.name == 'name' &&
  details.requested.version == '1.0') {
                details.useVersion 'replacedVersion'
           }
        }
    }
}

cheers, René