Lombok 编译器插件

The Lombok compiler plugin is Experimental. It may be dropped or changed at any time. Use it only for evaluation purposes. We would appreciate your feedback on it in YouTrack.

Lombok 编译器插件 - 图1

The Kotlin Lombok compiler plugin allows the generation and use of Java’s Lombok declarations by Kotlin code in the same mixed Java/Kotlin module. If you call such declarations from another module, then you don’t need to use this plugin for the compilation of that module.

The Lombok compiler plugin cannot replace Lombok, but it helps Lombok work in mixed Java/Kotlin modules. Thus, you still need to configure Lombok as usual when using this plugin. Learn more about how to configure the Lombok compiler plugin.

Supported annotations

The plugin supports the following annotations:

  • @Getter, @Setter
  • @Builder
  • @NoArgsConstructor, @RequiredArgsConstructor, and @AllArgsConstructor
  • @Data
  • @With
  • @Value

We’re continuing to work on this plugin. To find out the detailed current state, visit the Lombok compiler plugin’s README.

Currently, we don’t have plans to support the @SuperBuilder and @Tolerate annotations. However, we can consider this if you vote for @SuperBuilder and @Tolerate in YouTrack.

Kotlin compiler ignores Lombok annotations if you use them in Kotlin code.

Lombok 编译器插件 - 图2

Gradle

Apply the kotlin-plugin-lombok Gradle plugin in the build.gradle(.kts) file:

【Kotlin】

  1. plugins {
  2. kotlin("plugin.lombok") version "1.9.10"
  3. id("io.freefair.lombok") version "8.1.0"
  4. }

【Groovy】

  1. plugins {
  2. id 'org.jetbrains.kotlin.plugin.lombok' version '1.9.10'
  3. id 'io.freefair.lombok' version '8.1.0'
  4. }

See this test project with examples of the Lombok compiler plugin in use.

Using the Lombok configuration file

If you use a Lombok configuration file lombok.config, you need to set the file’s path so that the plugin can find it. The path must be relative to the module’s directory. For example, add the following code to your build.gradle(.kts) file:

【Kotlin】

  1. kotlinLombok {
  2. lombokConfigurationFile(file("lombok.config"))
  3. }

【Groovy】

  1. kotlinLombok {
  2. lombokConfigurationFile file("lombok.config")
  3. }

See this test project with examples of the Lombok compiler plugin and lombok.config in use.

Maven

To use the Lombok compiler plugin, add the plugin lombok to the compilerPlugins section and the dependency kotlin-maven-lombok to the dependencies section. If you use a Lombok configuration file lombok.config, provide a path to it to the plugin in the pluginOptions. Add the following lines to the pom.xml file:

  1. <plugin>
  2. <groupId>org.jetbrains.kotlin</groupId>
  3. <artifactId>kotlin-maven-plugin</artifactId>
  4. <version>${kotlin.version}</version>
  5. <configuration>
  6. <compilerPlugins>
  7. <plugin>lombok</plugin>
  8. </compilerPlugins>
  9. <pluginOptions>
  10. <option>lombok:config=${project.basedir}/lombok.config</option>
  11. </pluginOptions>
  12. </configuration>
  13. <dependencies>
  14. <dependency>
  15. <groupId>org.jetbrains.kotlin</groupId>
  16. <artifactId>kotlin-maven-lombok</artifactId>
  17. <version>${kotlin.version}</version>
  18. </dependency>
  19. <dependency>
  20. <groupId>org.projectlombok</groupId>
  21. <artifactId>lombok</artifactId>
  22. <version>1.18.20</version>
  23. <scope>provided</scope>
  24. </dependency>
  25. </dependencies>
  26. </plugin>

See this test project example of the Lombok compiler plugin and lombok.config in use.

Using with kapt

By default, the kapt compiler plugin runs all annotation processors and disables annotation processing by javac. To run Lombok along with kapt, set up kapt to keep javac’s annotation processors working.

If you use Gradle, add the option to the build.gradle(.kts) file:

  1. kapt {
  2. keepJavacAnnotationProcessors = true
  3. }

In Maven, use the following settings to launch Lombok with Java’s compiler:

  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-compiler-plugin</artifactId>
  4. <version>3.5.1</version>
  5. <configuration>
  6. <source>1.8</source>
  7. <target>1.8</target>
  8. <annotationProcessorPaths>
  9. <annotationProcessorPath>
  10. <groupId>org.projectlombok</groupId>
  11. <artifactId>lombok</artifactId>
  12. <version>${lombok.version}</version>
  13. </annotationProcessorPath>
  14. </annotationProcessorPaths>
  15. </configuration>
  16. </plugin>

The Lombok compiler plugin works correctly with kapt if annotation processors don’t depend on the code generated by Lombok.

Look through the test project examples of kapt and the Lombok compiler plugin in use:

Command-line compiler

Lombok compiler plugin JAR is available in the binary distribution of the Kotlin compiler. You can attach the plugin by providing the path to its JAR file using the Xplugin kotlinc option:

  1. -Xplugin=$KOTLIN_HOME/lib/lombok-compiler-plugin.jar

If you want to use the lombok.config file, replace <PATH_TO_CONFIG_FILE> with a path to your lombok.config:

  1. # The plugin option format is: "-P plugin:<plugin id>:<key>=<value>".
  2. # Options can be repeated.
  3. -P plugin:org.jetbrains.kotlin.lombok:config=<PATH_TO_CONFIG_FILE>