Gradle Multi-Module Project

Gradle is a powerful build tool that allows you to manage dependencies and build processes efficiently for your project. In this tutorial, we'll guide you through setting up a Gradle multi-module project using a sample "Blogger" project. We'll create a directory structure and configure the necessary build files.

Directory Structure

Here’s the directory structure we’ll create for our "Blogger" project:

├── gradle
├── gradlew
├── settings.gradle.kts
├── build-logic
│   ├── settings.gradle.kts
│   └── conventions
│       ├── build.gradle.kts
│       └── src/main/kotlin/shared-build-conventions.gradle.kts
├── blogger-core
│   └── build.gradle.kts
├── blogger-common
│   └── build.gradle.kts
├── blogger-web
│   └── build.gradle.kts
└── documentation
    └── build.gradle.kts

Step-by-Step Guide

Step 1: Create the Project Directory

First, create the main project directory and navigate into it.

mkdir blogger-project
cd blogger-project

Step 2: Create Subdirectories

Next, create the necessary subdirectories for the project.

mkdir -p build-logic/conventions/src/main/kotlin
mkdir blogger-core blogger-common blogger-web documentation
touch settings.gradle.kts
touch build-logic/settings.gradle.kts
touch build-logic/conventions/build.gradle.kts
touch build-logic/conventions/src/main/kotlin/shared-build-conventions.gradle.kts
touch blogger-core/build.gradle.kts
touch blogger-common/build.gradle.kts
touch blogger-web/build.gradle.kts
touch documentation/build.gradle.kts

Step 3: Initialize Gradle Wrapper

Initialize the Gradle wrapper in your project directory.

gradle wrapper

This will create the gradlew scripts and the gradle directory.

Step 4: Configure settings.gradle.kts

Configure the root settings file to include all modules.

rootProject.name = "blogger-project"

include(":build-logic")
include(":blogger-core")
include(":blogger-common")
include(":blogger-web")
include(":documentation")

Step 5: Configure build-logic/settings.gradle.kts

Set the name for the build logic project.

rootProject.name = "build-logic"

Step 6: Create Shared Build Conventions

Create shared build conventions in the build logic module.

build-logic/conventions/build.gradle.kts

plugins {
    `kotlin-dsl`
}

repositories {
    mavenCentral()
}

build-logic/conventions/src/main/kotlin/shared-build-conventions.gradle.kts

Define shared conventions for the project.

plugins {
    java
}

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.jetbrains.kotlin:kotlin-stdlib")
    testImplementation("junit:junit:4.13.2")
}

Step 7: Configure Sub-Modules

Configure each sub-module with its respective build.gradle.kts file.

blogger-core/build.gradle.kts

plugins {
    id("java")
}

repositories {
    mavenCentral()
}

dependencies {
    implementation(project(":blogger-common"))
    testImplementation("junit:junit:4.13.2")
}

blogger-common/build.gradle.kts

plugins {
    id("java")
}

repositories {
    mavenCentral()
}

dependencies {
    testImplementation("junit:junit:4.13.2")
}

blogger-web/build.gradle.kts

plugins {
    id("java")
    id("war")
}

repositories {
    mavenCentral()
}

dependencies {
    implementation(project(":blogger-core"))
    implementation(project(":blogger-common"))
    testImplementation("junit:junit:4.13.2")
}

documentation/build.gradle.kts

plugins {
    id("java")
}

repositories {
    mavenCentral()
}

dependencies {
    testImplementation("junit:junit:4.13.2")
}

Conclusion

In this guide, we have set up a Gradle multi-module project for the "Blogger" application. Each module has its own build.gradle.kts file, and we've created a shared build logic module to define common build conventions. This structure helps organize and manage the build process efficiently.

Feel free to customize and expand upon this structure to suit your project's specific needs. Happy coding!

Comments