How to create Gradle Multi Project for Java, Groovy Android?
Professionally we need to work with multiple modules of an Application. When all modules are individual, but they got dependencies each other, it’s become difficult to manage those. Some cases we have to use same types of dependencies in multiple modules, so there is big problem to manage dependencies version.
How do we solve the problem then? No worry Gradle is there, Gradle Multi project is an exciting technique to manage multiple Modules in same project.
Example Directory Structure of the project
gradle-multi-project/
├── gradle.properties
├── gradle
├── build.gradle
├── settings.gradle
├── gradlew
├── gradlew.bat
├── main-application/
├── application-modules/
│ ├── product
│ ├── order
│ └── customer
└── other-modules/
├── java-tm-util
├── java-lite-db-util
└── java-shell-util
Here
-
main-application: It’s for the parent application
-
application-modules: Other application modules will be store here.
-
other-modules: Some source modules comes for external sources.
-
gradle.properties: Here we will add some variables.
Create gradle multi project
Prerequisite
gradle.properties
Add the below variables into the gradle.properties
# Locations
mainProject=main-application/
applicationModules=application-modules/
otherModules=other-modules/
build.gradle
Add the below variables into the build.gradle, Please find description on video below
// Dependencies for buildscript
buildscript {
repositories {
mavenLocal()
mavenCentral()
jcenter()
maven { url "https://dl.bintray.com/hmtmcse/maven/" }
maven { url "https://repo.maven.apache.org/maven2/" }
}
dependencies {
classpath 'org.ajoberstar.grgit:grgit-core:3.1.1'
}
}
group = 'com.hmtmcse'
version = '0.0.1'
// Configuration for All Project.
allprojects {
repositories {
mavenLocal()
mavenCentral()
jcenter()
maven { url "https://dl.bintray.com/hmtmcse/maven/" }
maven { url "https://repo.maven.apache.org/maven2/" }
}
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'groovy'
sourceCompatibility = '11'
dependencies {}
}
// Some specific Configuration applied to Sub Project
subprojects { project ->
dependencies {}
}
// Project Mapping which will clone from repository
def projectDependencies = [
[
"name" : "Application Modules",
"destination" : applicationModules,
"dependencies": []
],
[
"name" : "Other Modules",
"destination" : otherModules,
"dependencies": [
"java-tm-util" : "https://github.com/hmtmcse/java-tm-util.git",
]
]
]
// Clone Project from GitHub Task
task resolveDependencies {
doLast {
String destinationPath = ""
projectDependencies.each { Map dependencyMap ->
destinationPath = dependencyMap.destination
println("Now Cloning ${dependencyMap.name}")
dependencyMap.dependencies.each { name, url ->
println("------------------------------------------------------------------------------------------")
def destination = file(destinationPath + name)
try {
println("Cloning Project ${name}")
org.ajoberstar.grgit.Grgit.clone(dir: destination, uri: url)
} catch (Exception e) {
println(e.getMessage())
}
println("------------------------------------------------------------------------------------------\n")
}
}
}
}
Here
If you run the above resolveDependencies It will automatically clone repositories from GitHub or others place and stored to Specific location which we mention at projectDependencies.
settings.gradle
Add the below variables into the settings.gradle, Please find description on video below
rootProject.name = 'GradleMultiProject' // Project Name
include "main-application" // Include Main Application
// Include Application Modules into project
String modulePath = "${applicationModules}"
File modulePathFile = file(modulePath)
if (modulePathFile.exists()) {
modulePathFile.list().each { name ->
if (name != "empty") {
include(name)
project(":${name}").projectDir = file(modulePath + name)
}
}
}
// Include External/Other Modules into project
modulePath = "${otherModules}"
modulePathFile = file(modulePath)
if (modulePathFile.exists()) {
modulePathFile.list().each { name ->
if (name != "empty") {
include(name)
project(":${name}").projectDir = file(modulePath + name)
}
}
}
How to add java-tm-util into Main Project after a clone?
Please add the below lines into you dependencies, it will automatically add the dependencies into your project.
implementation project (":java-tm-util")
For more clarification please see the video.