Gradle parent and sub-projects as siblings - gradle

In a gradle multi-module project, can the parent and sub-projects be siblings?
-parent
--build.gradle
--settings.gradle
-sub-project-1
--build.gradle
-sub-directory
--sub-module-2-1
---build.gradle
--sub-module-2-2
---build.gradle

Yes, it's possible to configure it this way, by declaring subprojects within settings.gradle as includeFlat instead of simple include. You can read about it here
But in that case, you have to run Gradle from the directory. where settings.gradle file is located, otherwise it won't find any subprojects.

Related

IntelliJ gradle integration issue with independent gradle subprojects

I have two independent gradle projects that I also want to build from one main gradle project:
main-project
build.gradle
settings.gradle
--subproject-one
build.gradle
settings.gradle
--module-a
build.gradle
--subproject-two
build.gradle
settings.gradle
--module-b
build.gradle
My top settings.gradle includes all modules from the sub projects:
file('subproject-one').eachDir { dir ->
if(dir.list().contains("build.gradle")) {
include dir.name
project(":${dir.name}").projectDir = dir
}
}
file('subproject-two').eachDir { dir ->
if(dir.list().contains("build.gradle")) {
include dir.name
project(":${dir.name}").projectDir = dir
}
}
Now i have a dependency from module-b in subproject-two to module-a in subproject-one. I use a compile switch (useProjectDependencies) to include them as project dependencies when I build from the main project and as normal dependencies when i build from the subprojects:
if(useProjectDependencies){
compile project(':module-b')
} else {
//Client dependencies
compile "my.group.id:module-b
}
Via gradle I'm now able to build the main-project including the subproject and also each subproject on it's own.
When I open the main-project now via IntelliJ I see all the modules are recognized correctly and also the cross references to classes between the modules work correctly (refactoring interface between to components are working great).
When I now try to use the gradle integration from Intellij and click on assemble for the module-b then it incorrectly uses the build.gradle form and settings.gradle from subproject-two and doesn't use the project dependencies. It looks like IntelliJ or the gradle daemon searches for the first settings.gradle in a bottom up way from the module-b instead of using the one in the main-project.
When I build via terminal from the main-project folder all works correctly:
gradlew :module-b:assemble //correct *settings.gradle* and *build.gradle* is used
When I delete the settings.gradle and build.gradle from subproject-two then the build.gradle from the main-project is correctly used.
Is there a way to specify the settings.gradle and build.gradle that is used as part of the IntelliJ gradle integration?

Having a multi-module project inside of another multi-module project in gradle

I have this question with regards to a gradle multi-module setup:
I have
root project
build.gradle
settings.gradle <-include 'subproject','multimodule'
multi module project
build.gradle
settings.gradle <- include multi1, multi2
multi1
build.gradle
multi2
build.gradle <- compile project(':multi1')
subproject
build.gradle <- How can this reference multi1?
settings.gradle
Now both subproject and multi2 needs to refer to multi1. multi2 is easily able to refer to multi1. But how do I make subproject refer multi1?
It looks like it is a limitation of Gradle that it does not support multiple root/sub-projects. This is because Gradle does not support the use of multiple settings.gradle files in a project setup. See this question. And this answer from me on a related question.

How to include projects in multiproject build from master directory in Gradle?

Lets have this structure
I am in subproject1 how to write my settings.gradle file to include subproject1 and subproject2 as subprojects of the root Project. My settings.gradle file is in master directory?
I tried:
include 'root:subproject1', 'root:subproject2'
but nothing happened.
From Gradle doc:
If you execute Gradle from within a project that has no settings.gradle file, Gradle does the following:
It searches for a settings.gradle in a directory called master which has the same nesting level as the current dir.
If no settings.gradle is found, it searches the parent directories for the existence of a settings.gradle file.
If no settings.gradle file is found, the build is executed as a single project build.
If a settings.gradle file is found, Gradle checks if the current project is part of the multiproject hierarchy defined in the found settings.gradle file. If not, the build is executed as a single project build. Otherwise a multiproject build is executed.
includeFlat 'subproject1', 'subproject2'
includeFlat is a short form for include-ing the projects and then setting their projectDirs as appropriate for a flat directory layout.
For more information, see the "Multi-project builds" chapter in the Gradle User Guide, and the sample builds in the full Gradle distribution.
Good project structure.
settings.gradle of the master directory should have the following include statements to have subproject1, subproject2 as a multi-module project.
rootProject.name = 'app-name'
includeFlat 'subproject1'
includeFlat 'subproject2'
There is no need to have settings.gradle inside subproject1, subproject2 modules.
version.gradle and settings.gradle can be saved best under the master module only.
subproject1, subproject2 can have build.gradle with specific dependencies section wrt to their projects. Common dependencies can be stated under build.gradle of master.
It is nice to have "gradle" with wrapper folder under the master, so that the latest version of the Gradle can be downloaded to build multi-module sub-projects. ( e.g: gradlew )

How to add other projects in current project in Gradle?

I have two projects ProjA and ProjB, here ProjB is depends on ProjA. So, how to include ProjA in ProjB?
And please let me know in case change in the ProjA build.gradle file?
Okay, so you have two projects, whereas ProjA depends on ProjB.
If you have already set up a gradle build for those, both of them will have a file settings.gradle, which at least defines the project name and build.gradle, where you can specify the dependencies.
Example:
settings.gradle
rootProject.name = 'ProjA'
build.gradle
group = 'mygroup'
version = '1.0'
By further adding one of Gradle's publishing plugins (see Maven publishing or Ivy publishing), you can configure a publication. A publication in turn contains artifacts, that is the files you want to upload (and usually some repositories).
Example for a minimal Maven publication:
apply plugin: 'maven-publish'
publishing {
publications {
core(MavenPublication) {
from components.java
}
}
}
The maven-publish plugin will add the task publishToMavenLocal (among others) to your project which installs the jar somewhere to ~/.gradle/caches/.
In build.gradle of ProjB you can then define a compile time dependency on ProjA like so:
dependencies {
compile(group: 'mygroup', name: 'ProjA', version: '1.0')
}
If your projects are co-located, you could have a multi build project.
Say you have a root folder with these contents:
some root directory
project-a
project-a.gradle
src
project-b
project-b.gradle
src
build.gradle
settings.gradle
settings.gradle contains:
include 'project-a', 'project-b'
project-b.gradle then depends on project A:
dependencies {
compile project(':project-a')
}
After this, project a will always compile and assemble before project-b and the jar file for project-a jar will be on the classpath when compiling project-b.

Nesting settings.gradle

Is it possible to nest multiple settings.gradle files for a multi module project?
E.g. settings.gradle of Project A should include settings.gradle of Project A_a
A (include ':A_a',':A_b')
A_a (include 'A_a_1','A_a_2','A_a_3')
A_a_1
A_a_2
A_a_3
A_b
No, there should only be one settings.gradle in the root project of the build. Gradle expects it to be that way, that's how it was built. It will
Please see the Build Lifecycle documentation of gradle for a full explanation. Particularly the Initialization chapter.

Resources