others-How to create multi-module java project using gradle kotlin DSL?

1. Purpose

In this post, I will demonstrate how to create a multi-module java project using gradle kotlin DSL?

2. Solution

1) Create a new Gradle project with the following command:

gradle init --type "java-library" --dsl "kotlin"

In the above command, I used the gradle build init plugin, which is:

The Build Init plugin can be used to create a new Gradle build. It supports creating brand new Gradle builds of various types as well as converting existing Apache Maven builds to Gradle.

The gradle build init plugin supports multiple init types as this document describes.

The java-libray type has following features:

java-library build type

The “java-library” build type is not inferable. It must be explicitly specified.

It has the following features:

* Uses the “java” plugin to produce a library implemented in Java

* Uses the “mavenCentral” dependency repository

* Uses JUnit 4 for testing

* Has directories in the conventional locations for source code

* Contains a sample class and unit test, if there are no existing source or test files

I use the option --dsl "kotlin" to generate build.gradle.kts script for root project and every module.

The init task also supports generating build scripts using either the Gradle Groovy DSL or the Gradle Kotlin DSL. The build script DSL defaults to the Groovy DSL for most build types and to the Kotlin DSL for Kotlin build types. The DSL can be selected by using the –dsl command-line option. For example, to create a Java library project with Kotlin DSL build scripts run: gradle init –type java-library –dsl kotlin.

2) Edit the project’s settings.gradle.kts file and add the modules you want to include in your project:

include("moduleA", "moduleB", "moduleC")

3) Create a subfolder for each module in the root directory and add a build.gradle.kts to each.

Do this for every module:

mkdir moduleA
cd moduleA
touch uild.gradle.kts

4) Edit the build.gradle.kts file in the root directory and add the following dependencies:

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

buildscript {
    repositories {
        mavenCentral()
    }
}

plugins {
    id("org.springframework.boot") version "2.7.5" apply false
    id("io.spring.dependency-management") version "1.0.15.RELEASE" apply false
    kotlin("jvm") version "1.7.20" apply false
    kotlin("plugin.spring") version "1.7.20" apply false
}

allprojects {
    group = "com.bswen"
    version = "1.0.0"

    tasks.withType<KotlinCompile> {
        kotlinOptions {
            freeCompilerArgs = listOf("-Xjsr305=strict")
            jvmTarget = "11"
        }
    }

    tasks.withType<Test> {
        useJUnitPlatform()
    }

}

subprojects {
    repositories {
        mavenCentral()
    }

    apply {
        plugin("io.spring.dependency-management")
    }
}

In the above script, we use jdk 11 to compile the project, and add dependencies of spring framework to submodules.

5) Finally, edit the build.gradle.kts files for each module and add the dependencies for each module.

For example in moduleA add the following content:

plugins {
    id("java")
    id("org.jetbrains.kotlin.jvm")
}

dependencies {
    implementation(project(":moduleB"))
    implementation(project(":moduleC"))
}

In the above script , we use implementation(project(":moduleC")) to depend directly on other modules in the same project.

6) Run

Now we can test the project by running the following command:

➜  bswen-kotlin-project2 git:(main) ✗ ./gradlew assemble
Downloading https://services.gradle.org/distributions/gradle-6.7.1-bin.zip
.........10%..........20%..........30%..........40%..........50%.........60%..........70%..........80%..........90%..........100%

BUILD SUCCESSFUL in 1m 53s
4 actionable tasks: 4 executed

we get this directory structure:

➜  bswen-kotlin-project2 git:(main) ✗ tree . -I 'build|test'
.
├── build.gradle.kts
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── moduleA
│   ├── build.gradle.kts
│   └── src
│       └── main
│           ├── java
│           │   └── bswen
│           │       └── kotlin
│           │           └── project2
│           │               └── Library.java
│           └── resources
├── moduleB
│   └── build.gradle.kts
│   └── src
│       └── main
│           ├── java
│           │   └── bswen
│           │       └── kotlin
│           │           └── project2
│           │               └── Library.java
│           └── resources
├── moduleC
│   ├── build.gradle.kts
│   └── src
│       └── main
│           ├── java
│           │   └── bswen
│           │       └── kotlin
│           │           └── project2
│           │               └── Library.java
│           └── resources
└── settings.gradle.kts

11 directories, 9 files

Now, you have created a multi-module Java project using Gradle Kotlin DSL.



3. Summary

In this post, I demonstrated how to use gradle init command to build a simple java project using kotlin DSL. That’s it, thanks for your reading.