Gradle add nested subproject from a multi-module project as dependency of another subproject
Asked Answered
C

2

26

I have a complex, but interesting situation. This is a tree diagram of my folder structure:

root
|___ settings.gradle
|___ p1
|___ p2  // depends on p3/sp1
|___ p3
|____|___sp1
|____|___sp2

I hope that explains the situation.

Now how would I add sp1 as a dependency of p2?

So far in my root setting.gradle, I have

rootProject.name = root
include 'p1'
include 'p2'
include 'p3'

In p2 build.gradle, I have:

dependencies {
    compile project (':p3:sp1')
}

But doing this still does not resolve the dependencies in p2; I still get errors about missing definition.

How do I fix this?

Just an aside, how would I resolve other dependencies sp1 might have. Like if it depends on sp2, do I need to declare this somehow even though it is already resolved within p3?

Complice answered 16/4, 2017 at 0:39 Comment(6)
Hi! Have you solve this problem? I am finding for example for resolve similar problemBlindfold
@Blindfold I think the accepted answer should work. Did you try it?Complice
Yep, I've already did same.Blindfold
By the way, I have the question. By default modules of my submodule use settings.gradle, build.gradle of parent project, but not the same files of submodel root project. Is there way to force using build.gradle and settings gradle of submodel root project?Blindfold
@Blindfold Gradle does not support having multiple settings.gradle in one gradle project (regardless of subprojects). You can use composite build projects to accomplish this thoughComplice
@Blindfold if your subprojects are not composite builds, then they should include their own build.gradle, but the parent project can configure each subproject by using the subproject closureComplice
M
31

Assuming project sp1 and sp2 are subprojects of project p3, if you want to do:

dependencies {
    compile project(':p3:sp1')
}

Then you need to change your settings.gradle to:

rootProject.name = root

include ':p1'
include ':p2'
include ':p3'    // Keep this if this folder contains a build.gradle
include ':p3:sp1'
include ':p3:sp2'
Madewell answered 16/4, 2017 at 2:3 Comment(1)
It's a bloody shame that Gradle has not documented this more properly. I find the tutorial at guides.gradle.org/creating-multi-project-builds misleading and the official documentation at docs.gradle.org/current/userguide/multi_project_builds.html missing some crucial information. With Maven multi-module builds are trivial (and recursive child modules are simple); with Gradle it's most definitely not.Indurate
I
8

Just to add the answer for those who might be looking, in your settings.gradle you will have

include ':p1',':p2',':p3:sp1',':p3:sp2'

if sp1 depends on sp2

then add dependency on sp1's gradle as

dependency {
   compile project(":p3:sp2")
}
Incautious answered 1/6, 2018 at 3:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.