项目作者: rojiani

项目描述 :
How to Use Kotlin with Lombok in a mixed Kt/Java project
高级语言: Kotlin
项目地址: git://github.com/rojiani/kotlin-lombok-integration.git
创建时间: 2018-01-20T07:53:51Z
项目社区:https://github.com/rojiani/kotlin-lombok-integration

开源协议:

下载


Kotlin + Java/Lombok Build Demo

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.

References

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).

Kotlin - Using Gradle

Running the app

Gradle

Run Main.kt:

  1. $ cd kotlin-lombok-integration
  2. $ ./gradlew build
  3. $ ./gradlew run
  4. > Task :kotlin-subproject:run
  5. Hi Harry and Ron and Hermione
  6. BUILD SUCCESSFUL in 0s
  7. 4 actionable tasks: 1 executed, 3 up-to-date

Run Tests:

  1. ( ) at Fri 1.19.18 in kotlin-lombok-integration on master [!]
  2. ❯❯❯ ./gradlew test
  3. > Task :kotlin-subproject:test
  4. demo.GreetingJoinerTest > getJoinedGreeting PASSED
  5. BUILD SUCCESSFUL in 2s
  6. 5 actionable tasks: 2 executed, 3 up-to-date

IntelliJ

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).