龙目岛和模块-info.java

Lombok and module-info.java

我正在将现有的 Java 库转换为使用模块-info.java。我广泛使用 Lombok 和泛型。当我 运行 “mvn package” 时,我收到大量遵循此模式的错误:

[ERROR] /projects/app/src/main/java/com/whatever/app/server/handlers/HandlerCreateForumPost.java:[116,13] cannot find symbol
[ERROR]   symbol:   method <com.whatever.app.common.messages.CreateForumPostResults>builder()
[ERROR]   location: class com.whatever.app.common.Response

导致错误的语句是:

return Response
    .<CreateForumPostResults>builder()
    .success(true)
    .requestID(context.getRequestID())
    .results(
        CreateForumPostResults
            .builder()
            .id(forumPostID)
            .build()
    )
    .build();

报错的第116行是“.builder()”这一行。

响应 class 存在于 com.whatever.app.common 中,定义为:

package com.whatever.app.common;

import lombok.Getter;
import lombok.Setter;
import lombok.experimental.SuperBuilder;
import lombok.extern.jackson.Jacksonized;

@SuperBuilder
@Jacksonized
@Getter
@Setter
public class Response<T extends Response.Results> {
    boolean success;
    String message;
    String requestID;
    T results;

    @SuperBuilder
    @Jacksonized
    @Getter
    @Setter
    public static class Results {

    }
}

CreateForumPostResults class 位于 com.whatever.app.common.messages 并扩展 Response.Results:

package com.whatever.app.common.messages;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.whatever.app.common.AppResponse;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.SuperBuilder;
import lombok.extern.jackson.Jacksonized;

import java.util.UUID;

@SuperBuilder
@Getter
@Setter
@Jacksonized
public class AppCreateForumPostResults extends AppResponse.Results {
    @JsonProperty("id")
    UUID id;
}

我的模块-info.java 看起来像这样:

module com.whatever.app.common {
    exports com.whatever.app.common;
    exports com.whatever.app.common.messages;

    requires com.fasterxml.jackson.annotation;
    requires com.fasterxml.jackson.core;
    requires com.fasterxml.jackson.databind;
    requires com.fasterxml.jackson.datatype.jsr310;
    requires com.google.common;
    requires lombok;
}

我的POM如下:

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

    <parent>
        <artifactId>app</artifactId>
        <groupId>com.whatever</groupId>
        <version>0.9</version>
    </parent>

    <artifactId>app-common</artifactId>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <maven.compiler.plugin.version>3.8.1</maven.compiler.plugin.version>

        <maven.jar.plugin.version>3.2.0</maven.jar.plugin.version>

        <guava-version>23.0</guava-version>

        <jackson-version>2.13.1</jackson-version>

        <lombok-version>1.18.22</lombok-version>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven.compiler.plugin.version}</version>
                <configuration>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>${maven.jar.plugin.version}</version>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson-version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jsr310</artifactId>
            <version>${jackson-version}</version>
        </dependency>

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>${guava-version}</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok-version}</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
</project>

我怀疑需要一些神奇的咒语才能使 Lombok 的 @Builder 注释(可能还有其他注释)与模块 info.java 一起工作,或者可能与泛型相关的某些东西导致它中断,但我还没有没能弄清楚它是什么。

我做错了什么?

非常感谢任何见解。

通常情况下,花时间把这个问题写出来清楚地引导我找到我正在寻找的答案。简版,我需要修改maven-compiler-plugin配置如下:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven.compiler.plugin.version}</version>
            <configuration>
                <source>${maven.compiler.source}</source>
                <target>${maven.compiler.target}</target>
                <annotationProcessorPaths>
                    <annotationProcessorPath>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                        <version>${lombok-version}</version>
                    </annotationProcessorPath>
                </annotationProcessorPaths>
            </configuration>
        </plugin>