Using Kotlin in an existing Java project that uses Kotlin has caused a lot of problems for people, with several questions on StackOverflow & elsewhere (see 1, 2, 3, 4, 5, 6, 7)
Looking through it, there were some suggestions of it being possible using Kapt &/or Delombok, but not much of clear guidance, until I found this answer:
If you put Java and Kotlin code in different modules, it should work. Rationale: Kotlin will see whatever declarations get to the byte code, but it can’t see whatever Lombok generates on-the-fly in the source code
This project shows a Hello World app using a Gradle multi-project build, but it should also work for Maven if you split the Java & Kotlin into separate modules.
For more info on Kotlin+Lombok, see this Medium Post.
This hello-world app is an adapted version of JetBrains' mixed-java-kotlin-hello-world example.
See the Gradle Tutorial for more info on multi-project builds (& code on GitHub).
Run Main.kt
:
$ cd kotlin-lombok-integration
$ ./gradlew build
$ ./gradlew run
> Task :kotlin-subproject:run
Hi Harry and Ron and Hermione
BUILD SUCCESSFUL in 0s
4 actionable tasks: 1 executed, 3 up-to-date
Run Tests:
( ) at Fri 1.19.18 in kotlin-lombok-integration on master [!]
❯❯❯ ./gradlew test
> Task :kotlin-subproject:test
demo.GreetingJoinerTest > getJoinedGreeting PASSED
BUILD SUCCESSFUL in 2s
5 actionable tasks: 2 executed, 3 up-to-date
Importing: Import Project
-> Import project from external model
-> Gradle
Note: In the IntelliJ preferences, make sure you enable the Lombok plugin & annotation processing for the project.
Open Main.kt
. Click the run/start symbol & select Run demo.MainKt
. Kotlin code should be able to call any Lombok-generated methods on Java classes (e.g., "${greeter.greeting} ..."
in GreetingJoiner.kt
).