Gradle - build subprojects subprojects - gradle

Assume I have a Gradle project that looks like
RootProject
|-- SubProject1
| |- SubProject1A
| | `- build.gradle
| `- SubProject1B
| `- build.gradle
|-- SubProject2
| |- SubProject2A
| | `- build.gradle
| `- SubProject2B
| `- build.gradle
|- gradle.build
`- settings.gradle
Is it possible to build a subprojects all subprojects? I want to run
gradle :SubProject1:build
But it doesn't build the subprojects SubProject1A and SubProject1A
My settings.gradle looks like
include ":SubProject1:SubProject1A"
include ":SubProject1:SubProject1B"
include ":SubProject2:SubProject2A"
include ":SubProject2:SubProject2A"
How can I build a subprojects all subprojects?

I might be wrong, but I believe you also need a build.gradle in SubProject1 and SubProject2
Edit:
I found the solution.
Create a build.gradle in SubProject1 and SubProject2 and add a task dependency to SubProject1A and SubProject1B etc like this:
build.dependsOn ':SubProject1:SubProject1A:build', ':SubProject1:SubProject1B:build'
You can then run :SubProject1:build and it will also execute :SubProject1:SubProject1A:build and :SubProject1:SubProject1B:build

Related

Gradle: exclude build.gradle.kts in a subdirectory

Is there a way to tell Gradle
"Hey, there's a build.gradle.kts in the directory some/subdir, but please act like it's not there and don't try to mess with it at all"?
I have to keep a non-Android Kotlin project in an Android repository with a build.gradle.kts-file in the repository root for ... reasons ... and it keeps breaking my build due to plugin version conflicts.
The hierarchy layout is as follows:
|-- build.gradle.kts
|-- settings.gradle.kts
|-- src/
|-- android_project_a/
|-- build.gradle.kts
|-- android_project_b/
|-- build.gradle.kts
|-- kotlin_project/
|-- build.gradle.kts // This is what I need to exclude
If you are using command line to build, You can execute subdir as following
gradle build -x kotlin_project:build
But this is not a nice way to have it done, I wanted to check your build.gradle and setting.gradle, As you can change that once and for all, If you change the sub-projects and implementation of your sub-projects.
This kotlin_project should be a part of your setting.gradle as its being build automatically when you trigger root dir build.
You can exclude it if you want, But you have to remove it as well from dependencies at your build.gradle

How to include another root project in settings.gradle

I have a 2 root projects in gradle one main root project1 from where I invoke gradle build and has settings.gradle which includes subprojects to build
Another root project2 where I have settings.gradle which includes subprojects to build
Rootproject1
build.gradle
settings.gradle
Rootproject2
build.gradle
settings.gradle
I want to inlcude Rootproject2 in my Rootproject1 settings.gradle file so that from Rootproject1 (gradle build) should build Rootproject1 as well as Rootproject2
include/inlcudeFlat -- it is relative to rootproject
There should be one settings.gradle which will be placed inside the primary root folder and you will need to write:
include ':Rootproject1',
':Rootproject2'
Then, in the build.gradle file of Rootproject1, you will need to write:
dependencies {
implementation project(path: ':Rootproject2')
}
[Updated on Jan 23, 2019] Something like following:
Root
|------ Rootproject1
|
|------ Rootproject2
|
|------ settings.gradle

Share dependency versions between Maven (pom.xml) and Gradle (build.gradle)

I have projects built by Maven and Gradle. Is there a way to define dependency versions in a text file, e.g.:
.
|-- dep-versions.properties
|
|-- proj-by-gradle
| |-- build.gradle
| `-- settings.gradle
|
`-- proj-by-maven
`-- pom.xml
Is there an easy way to specify in dep-versions.properties, something like:
com.google.guava:guava = 26.0-jre
org.apache.commons:commons-pool2 = 2.5.0
And then use these versions in both the pom.xml and build.gradle?
You can create a BOM pom (Bill of materials) and use it in both builds. Support for importing maven BOM files was added to Gradle in version 4.6: https://docs.gradle.org/4.6/release-notes.html#bom-import

Gradle multiproject doesn't add projects to classpath

I have some projects that depend on others. I also have a project that depends on two projects that each one depends on the same project. Something like this:
-Project A
* Project 1
** Project C
* Project 2
** Project C
And the structure of the workspace is like this:
-ProjectA
-ProjectC
-Project1
-Project2
All the projects are at the same level.
So in the settings.gradle in my Project A I have:
include ':Project1',':Project1:ProjectC',[...]
project(':Project1') = new File('../Project1')
project(':Project2') = new File('../Project2')
project(':Project1:ProjectC') = new File('../ProjectC')
project(':Project2:ProjectC') = new File('../ProjectC')
And in the build.gradle I do:
dependencies{ compile project('Project1'),project('Project2')
The problem is that it is not correctly added to the classpath. I think since both Project1 and Project2 depends on ProjectC it is overwritten somehow. Any ideas?
Thanks.
EDIT
Here is the tree of dependencies:
Root project 'ProjectA'
+--- Project ':ProjectB'
| \--- Project ':ProjectB:Project1'
| +--- Project ':ProjectB:Project1:Project2'
| \--- Project ':ProjectB:Project1:Project3'
\--- Project ':ProjectC'
\--- Project ':ProjectC:Project1'
+--- Project ':ProjectC:Project1:Project2'
\--- Project ':ProjectC:Project1:Project3'
For a workspace that looks like this:
rootFolder
|
|- build.gradle
|- settings.gradle
|
|- ProjectA
| |-build.gradle
|
|- Project1
| |-build.gradle
|
|- Project2
| |-build.gradle
|
|- ProjectC
|-build.gradle
Your settings.gradle should look like this (irrespective of dependency relationships of the sub projects):
include ':ProjectA',':Project1',':Project2',':ProjectC',
You're just telling the root project that there are 4 subprojects and where they are located. That's it.
Now the dependency relationships are handled inside each subproject's build.gradle files. For a dependency relationship that looks like this:
ProjectA
|-Project1
| |-ProjectC
|
|-Project2
|-ProjectC
ProjectA's build.gradle:
dependencies{
compile project(':Project1')
compile project(':Project2')
}
Project1's build.gradle:
dependencies{
compile project(':ProjectC')
}
Project2's build.gradle:
dependencies{
compile project(':ProjectC')
}
What I finally did is change in the classpath the paths that where wrong like this:
build.gradle
eclipse{
classpath{
file{
whenMerged { classpath ->
classpath.entries.find { entry ->
entry.kind == 'src' && entry.path.contains('ProjectC')
}.each{ entry ->
entry.path=file("/ProjectC")
}
}
}
}
}
And it works fine. I still don't know why gradle doesn't make the classpath correctly...

Gradle Multi-Project Build and Encapsulation

I'm using gradle as build engine for multiple projects. I have the following dependency between the projects:
- MobileApp
|
+-- CordovaPlugin
|
+--Docs
|
+--NativeiOSImpl
| |
| +--Docs
+--NativeAndroidImpl
|
+--Docs
Each of the projects is a project on it own so each project has it own build.gradle file and settings.gradle.
My problem is that I have to repeat the settings.gradle in each level.
Am I missing something or there is no encapulation of a project in gradle?

Resources