Jay
Jay

Reputation: 9479

Gradle - Multi projects - flat directory structure all at same level

Gradle multi individual dependent Java projects. With flat folder structure all at same levels.

I am trying to create multi gradle Java projects like this.

I am able to make it work only with the below configurations. I am not sure if this is the right approach to achieve the above requirements.

Child2 - settings.gradle

include ':Child1'
project(':Child1').projectDir = new File(settingsDir, "../Child1"); 

Child2 - build.gradle

dependencies {
    compile project(':Child1')
}

Parent - settings.gradle

include ':Child1', ':Child2'
project(':Child1').projectDir = new File(settingsDir, "../Child1");
project(':Child2').projectDir = new File(settingsDir, "../Child2");

Note:- I wonder why should we define Child1 details here ?? Because Parent just needs the Child2 dependency. If I don't configure like above it does not works.

Parent - build.gradle

dependencies {
    compile project(':Child2')
}

enter image description here

Upvotes: 1

Views: 3101

Answers (1)

Jose Martinez
Jose Martinez

Reputation: 11992

In a multi module project the child project's directories are sub-folders in the parent folder. This is by convention. If you align your folders in that convention then there is less information that needs to go into your build files.

Below is an example from this question.

myproject
    |
|-- ear
    |-- build.gradle
    |-- ...
|-- core
    |-- build.gradle
    |-- ...
|-- web
    |-- build.gradle
    |-- include core project as dependency for this project
    |-- ...
|-- settings.gradle // has include "ear", "core", "web"

Since your structure is not in this convention, then you need to be explicit about the location of the children. The relationship between the parent and child1 is direct, unlike the relationship of a dependency library and other dependencies that that library might have. If child1 was just a dependency of child2, and not a child of parent, then you would not have had to point out the folder of chil1. But it is the case that 1) child1 is a child of parent and 2) you ar enot using standard directory layout for multi-module. so now you have to tell Gradle where child1 is located in parent's build script.

Upvotes: 1

Related Questions