Kotlin Help

Lombok compiler plugin

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 make the plugin seeing Lombok's config.

Supported annotations

The plugin supports the following annotations:

  • @Getter, @Setter

  • @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 @Builder annotation. However, we can consider this if you vote for @Builder in YouTrack.

Gradle

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

plugins { kotlin("plugin.lombok") version "1.5.31" id("io.freefair.lombok") version "5.3.0" }
plugins { id 'org.jetbrains.kotlin.plugin.lombok' version '1.5.31' id 'io.freefair.lombok' version '5.3.0' }

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, provide a path to it to the plugin. The path should be relative to the module's directory. Add the following code to your build.gradle(.kts) file:

kotlinLombok { lombokConfigurationFile(file("lombok.config")) }
kotlinLombok { lombokConfigurationFile file("lombok.config") }

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:

<plugin> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-maven-plugin</artifactId> <version>${kotlin.version}</version> <configuration> <compilerPlugins> <plugin>lombok</plugin> </compilerPlugins> <pluginOptions> <option>lombok:config=${project.basedir}/lombok.config</option> </pluginOptions> </configuration> <dependencies> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-maven-lombok</artifactId> <version>${kotlin.version}</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.20</version> <scope>provided</scope> </dependency> </dependencies> </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:

kapt { keepJavacAnnotationProcessors = true }

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

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <source>1.8</source> <target>1.8</target> <annotationProcessorPaths> <annotationProcessorPath> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> </annotationProcessorPath> </annotationProcessorPaths> </configuration> </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:

Last modified: 08 September 2021