Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring kapt to process lombok annotations

This is a followup for these questions:

  • Kotlin doesn't see Java Lombok accessors?
  • Is kapt supported in maven?

It seems that the kapt has evolved since and now it is even supported in Maven. I am trying this (note the Lombok annotation processor in the config):

       <plugin>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-maven-plugin</artifactId>
            <version>${kotlin.version}</version>
            <executions>
                <execution>
                    <id>kapt</id>
                    <goals>
                        <goal>kapt</goal>
                    </goals>
                    <configuration>
                        <sourceDirs>
                            <sourceDir>${project.basedir}/src/main/java</sourceDir>
                            <sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
                        </sourceDirs>
                        <annotationProcessors>
                            <annotationProcessor>lombok.core.AnnotationProcessor</annotationProcessor>
                        </annotationProcessors>
                    </configuration>
                </execution>
                ...
       </plugin>

But it still seems to have no effect and the Lombok @Getter is still ignored as described in the related issues.

Is there anything that can be done?

like image 304
Vojtěch Avatar asked May 25 '17 12:05

Vojtěch


2 Answers

So, if you wish to use Lombok annotations on Kotlin classes, this should work:

<execution>
    <id>kapt</id>
    <goals>
        <goal>kapt</goal>
    </goals>
    <configuration>
        <sourceDirs>
            <sourceDir>src/main/kotlin</sourceDir>
            <sourceDir>src/main/java</sourceDir>
        </sourceDirs>
        <annotationProcessors>
            <annotationProcessor>lombok.launch.AnnotationProcessorHider$AnnotationProcessor</annotationProcessor>
        </annotationProcessors>
        <annotationProcessorPaths>
            <annotationProcessorPath>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${lombok.version}</version>
            </annotationProcessorPath>
        </annotationProcessorPaths>
    </configuration>
</execution>

If you need to use Lombok classes in Kotlin code, you need to use delombok:

<plugin>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok-maven-plugin</artifactId>
    <version>${lombok.version}.0</version>
    <executions>
        <execution>
            <id>delombok</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>delombok</goal>
            </goals>
            <configuration>
                <formatPreferences>
                    <javaLangAsFQN>skip</javaLangAsFQN>
                </formatPreferences>
                <verbose>true</verbose>
            </configuration>
        </execution>
        <execution>
            <id>test-delombok</id>
            <phase>generate-test-sources</phase>
            <goals>
                <goal>testDelombok</goal>
            </goals>
            <configuration>
                <verbose>true</verbose>
            </configuration>
        </execution>
    </executions>
</plugin>

In this case you don't need kapt at all. Works like a charm for me.

like image 179
Pavel S. Avatar answered Oct 04 '22 03:10

Pavel S.


after lots of googling, i came up with this solution which allows lombok-enhanced java and kotlin code in the same module to call each other:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.ericytsang</groupId>
    <artifactId>playground</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>com.ericytsang playground</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <kotlin.version>1.2.50</kotlin.version>
        <junit.version>4.12</junit.version>
        <lombok.version>1.16.8</lombok.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-stdlib</artifactId>
            <version>${kotlin.version}</version>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-test-junit</artifactId>
            <version>${kotlin.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>

            <plugin>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok-maven-plugin</artifactId>
                <version>${lombok.version}.0</version>
                <executions>
                    <execution>
                        <id>delombok</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>delombok</goal>
                        </goals>
                        <configuration>
                            <sourceDirectory>src/main/java</sourceDirectory>
                            <outputDirectory>${project.build.directory}/delombok-main</outputDirectory>
                            <addOutputDirectory>false</addOutputDirectory>
                        </configuration>
                    </execution>
                    <execution>
                        <id>delombok-test</id>
                        <phase>generate-test-sources</phase>
                        <goals>
                            <goal>delombok</goal>
                        </goals>
                        <configuration>
                            <sourceDirectory>src/test/java</sourceDirectory>
                            <outputDirectory>${project.build.directory}/delombok-test</outputDirectory>
                            <addOutputDirectory>false</addOutputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-maven-plugin</artifactId>
                <version>${kotlin.version}</version>
                <executions>
                    <execution>
                        <id>compile</id>
                        <phase>process-sources</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                        <configuration>
                            <sourceDirs>${project.build.directory}/delombok-main</sourceDirs>
                        </configuration>
                    </execution>
                    <execution>
                        <id>test-compile</id>
                        <phase>test-compile</phase>
                        <goals>
                            <goal>test-compile</goal>
                        </goals>
                        <configuration>
                            <sourceDirs>${project.build.directory}/delombok-test</sourceDirs>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

</project>

after implementing this solution, i revised it multiple times.

  1. i want to keep my source code in src/main/java instead of src/main/lombok, so i added the <sourceDirectory>src/main/java</sourceDirectory> elements in the lombok configurations.
  2. adding the above configurations caused duplicate class compile errors because the compiler was compiling the delomboked code as well as the code in src/main/java...i only want it to compile the delomboked code...but i also want the IDE to ignore the delomboked code....so i added <addOutputDirectory>false</addOutputDirectory> to lombok plugin, and <sourceDirs>${project.build.directory}/delombok-main</sourceDirs> to the kotlin plugin.

helpful discussion on delomboking code

like image 28
Eric Avatar answered Oct 04 '22 03:10

Eric