Key Concepts:
1. Root Project: This is the top-level project that serves as the parent of all subprojects. The root project typically contains the main settings.gradle (or settings.gradle.kts for Kotlin DSL) file that defines which subprojects are included in the build.
2. Subprojects: These are individual modules or projects that make up the larger multi-project build. Each subproject can have its own build script (build.gradle), but they can also inherit configurations and dependencies from the root project.
Structure of a Gradle Multi-Project Build:
1. Root Project:
Contains a settings.gradle file that includes references to all subprojects.
Contains a build.gradle file where common configurations (like plugins or dependency management) can be applied to all subprojects.
2. Subprojects:
Each subproject can have its own build.gradle file.
Subprojects can depend on each other and can be built independently or together.
Example Structure:
my-multi-project/
│
├── settings.gradle
├── build.gradle
├── subprojectA/
│ └── build.gradle
└── subprojectB/
└── build.gradle
Important Files:
1. settings.gradle: Defines which subprojects are part of the build. This file resides in the root directory and looks like this:
include 'subprojectA', 'subprojectB'
2. Root build.gradle: You can configure shared dependencies or tasks here, which apply to all subprojects:
allprojects {
repositories {
mavenCentral()
}
}
subprojects {
dependencies {
testImplementation 'junit:junit:4.12'
}
}
3. Subproject build.gradle: Each subproject can define its own specific configurations and dependencies. For example:
apply plugin: 'java'
dependencies {
implementation 'org.springframework:spring-core:5.0.0'
}
Benefits of a Multi-Project Build:
1. Modularity: Breaking down a large application into smaller, manageable modules that can be built and tested independently.
2. Dependency Management: Easily manage inter-project dependencies (e.g., subprojectA depends on subprojectB).
3. Task Management: You can run tasks across all subprojects or specific ones. For example, running ./gradlew build will build all subprojects, while ./gradlew :subprojectA:build will only build subprojectA.
Dependency Declaration Between Subprojects:
In a multi-project build, subprojects can depend on each other. For example, if subprojectA depends on subprojectB, you would declare this dependency in subprojectA/build.gradle:
dependencies {
implementation project(':subprojectB')
}
Running Tasks in a Multi-Project Build:
Run tasks for all subprojects: When you run a Gradle task like ./gradlew build, Gradle will run that task across all subprojects.
Run tasks for a specific subproject: You can target a specific subproject with a task by using the following command:
./gradlew :subprojectA:build
Conclusion:
Gradle's multi-project builds are powerful for managing large codebases, enabling modularity, and simplifying build and dependency management across projects.
Rate This Article
Thanks for reading: gradle multi-project setup explained, Sorry, my English is bad:)