配置kapt以处理lombok注释

时间:2017-05-25 12:10:49

标签: kotlin kapt

这是以下问题的后续内容:

似乎kapt已经发展,现在它甚至在Maven中得到支持。我正在尝试这个(请注意配置中的Lombok注释处理器):

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

但它似乎仍无效,Lombok @Getter仍然被忽略,如相关问题所述。

有什么可以做的吗?

2 个答案:

答案 0 :(得分:2)

因此,如果您希望在Kotlin类上使用Lombok注释,这应该有效:

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

如果您需要在Kotlin代码中使用Lombok类,则需要使用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>

在这种情况下,你根本不需要kapt。对我来说就像是一种魅力。

答案 1 :(得分:0)

经过大量的谷歌搜索,我想出了这个解决方案,允许在同一个模块中使用lombok增强的java和kotlin代码互相调用:

<?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>

实施this solution后,我多次修改。

  1. 我想将源代码保存在src/main/java而不是src/main/lombok,所以我在lombok配置中添加了<sourceDirectory>src/main/java</sourceDirectory>个元素。
  2. 添加上述配置导致重复的类编译错误,因为编译器正在编译删除代码以及src/main/java中的代码...我只希望它编译删除代码...但我也想要IDE忽略了删除代码....所以我将<addOutputDirectory>false</addOutputDirectory>添加到lombok插件,并将<sourceDirs>${project.build.directory}/delombok-main</sourceDirs>添加到kotlin插件。
  3. helpful discussion on delomboking code