Adding Kotlin to Java + Spring Boot Project

Adding Kotlin to Java + Spring Boot Project

Kotlin?

  • Kotlin is a development language created by JetBrains, well-known for producing IntelliJ IDEA. It can be said to be a newly created language by a group of experts with deep knowledge in the Java language, aimed at maintaining enterprise-level applications with minimal effort and ease of maintenance. After several years of beta testing, version 1.0 was first released in 2016, and as of the time of writing, version 1.9.23 has been released.

Java and Kotlin in the same project?

  • Kotlin shares the same JVM ecosystem as Java. Just as Java source code is compiled into the intermediate language .class by javac, Kotlin source code is also compiled into .class by kotlinc. The two compiled files are executed identically without discrimination by the JVM. With the help of modern development tools such as Gradle and IntelliJ IDEA, we can conveniently write in both languages coexisting in the same project.

First Steps in Adding Kotlin

  • IntelliJ IDEA, true to the creators of Kotlin, provides a Convert Java File to Kotlin feature that allows you to automatically convert .java files to .kt with just one click. (Right-click on the Java file you want to convert in the project tree to see this feature.) It's not completely perfect, so depending on the size and complexity of the source code, some manual modifications might be necessary after the conversion. Running this feature for the first time not only converts the source file but also automatically updates the build.gradle file, making it incredibly convenient.

Spring Boot 3 + Spring Data JPA + Querydsl

  • For relatively simple projects, running the Convert Java File to Kotlin feature and a few manual tasks might be sufficient. Additionally, to incorporate Kotlin into one of the most popular technology stacks in backend development, Spring Boot 3 + Spring Data JPA + Querydsl, simply adding the following content to the build.gradle file in the project root will make everything work perfectly.
# /gradle/wrapper/gradle-wrapper.properties
...
kotlinVersion=1.9.23
springBootVersion=3.2.4
querydslVersion=5.1.0

# /build.gradle
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

buildscript {
    ext {
        kotlinVersion = '1.9.23'
        springBootVersion = '3.2.4'
        querydslVersion = '5.1.0'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}")
        classpath("org.jetbrains.kotlin:kotlin-allopen:${kotlinVersion}")
        classpath("org.jetbrains.kotlin:kotlin-noarg:${kotlinVersion}")
    }
}

plugins {
    ...
    id 'org.jetbrains.kotlin.jvm' version "${kotlinVersion}"
    id 'org.jetbrains.kotlin.plugin.spring' version "${kotlinVersion}"
    id 'org.jetbrains.kotlin.plugin.jpa' version "${kotlinVersion}"
    id 'org.jetbrains.kotlin.kapt' version "${kotlinVersion}"
    id 'org.jetbrains.kotlin.plugin.lombok' version "${kotlinVersion}"
    id 'io.freefair.lombok' version '8.6'
    id 'idea'
}

dependencies {
    ...
    implementation 'org.jetbrains.kotlin:kotlin-reflect'
    implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
    implementation 'com.fasterxml.jackson.module:jackson-module-kotlin'
    // Disable existing Java parts when applying Querydsl
    // annotationProcessor "com.querydsl:querydsl-apt:${querydslVersion}:jakarta"
    kapt "com.querydsl:querydsl-apt:${querydslVersion}:jakarta"
}

kapt {
    keepJavacAnnotationProcessors = true
}

tasks.withType(KotlinCompile).configureEach {
    kotlinOptions {
        freeCompilerArgs += '-Xjsr305=strict'
        jvmTarget = '21'
    }
}

References