Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 414
smac89 Avatar asked Apr 16 '17 00:04

smac89


People also ask

What is subproject in gradle?

The subproject producer defines a task named buildInfo that generates a properties file containing build information e.g. the project version. You can then map the task provider to its output file and Gradle will automatically establish a task dependency. Example 2.

What is a multi module project gradle?

A multi-project build in Gradle consists of one root project, and one or more subprojects. A basic multi-project build contains a root project and a single subproject. This is a structure of a multi-project build that contains a single subproject called app : Example 1.


2 Answers

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'
like image 178
Jared Burrows Avatar answered Oct 21 '22 01:10

Jared Burrows


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")
}
like image 6
andylamax Avatar answered Oct 21 '22 02:10

andylamax