我试图在我的项目中使用Lombok,我正在使用IntelliJ IDEA 11开发。

我已经为IDEA安装了第三方插件,它似乎工作正常,因为IDEA可以看到所有自动生成的方法/字段。

我有一个使用Slf4j的类。我这样做了注解

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class TestClass
{
    public TestClass()
    {
        log.info("Hello!");
    }
}

但当我建立我的项目编译器吐槽:无法找到符号变量日志。

你能告诉我我错过了什么吗?

更新:原来是RequestFactory注释过程失败了。

input files: {com.zasutki.courierApp.server.TestServlet, com.mine.courierApp.server.model.DatastoreObject}

annotations: [javax.inject.Singleton, javax.inject.Inject, lombok.Getter, lombok.Setter, com.googlecode.objectify.annotation.Id, com.googlecode.objectify.annotation.OnSave]

Processor com.google.web.bindery.requestfactory.apt.RfValidator matches [lombok.Getter, com.googlecode.objectify.annotation.Id, javax.inject.Inject, lombok.Setter, com.googlecode.objectify.annotation.OnSave, javax.inject.Singleton] and returns false.

cannot find symbol variable log

有什么解决办法吗?

更新2:也许这不是读者想听到的,但我最终转向了Scala。


当前回答

在pom.xml中包含以下内容对我来说是有效的:

<build>
        <defaultGoal>spring-boot:run</defaultGoal>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <annotationProcessorPaths>
...
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>${lombok.version}</version>
                        </path>       
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
</build>

其他回答

除了在所有答案中提到的,我必须在pom.xml配置中添加以下代码,以使mvn清洁安装工作。在添加这段代码之前,我得到了getter和setter的无法找到符号。

                    <annotationProcessorPath>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                        <version>1.18.8</version>
                    </annotationProcessorPath>

对于那些仍然有问题的人:

除了上述启用注释处理器和安装IntelliJ Lombok插件的步骤之外,我还必须进行Build -> Rebuild Project。

我必须禁用刚才添加的@Document注释。我正在转换我的项目使用Mongo而不是Postgres,它以前是工作的,但似乎@文档与Lombok的@Getter冲突

在pom.xml中包含以下内容对我来说是有效的:

<build>
        <defaultGoal>spring-boot:run</defaultGoal>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <annotationProcessorPaths>
...
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>${lombok.version}</version>
                        </path>       
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
</build>

我想我还没读到答案里的最后一步。(Mac + IntelliJ终极版2020.1) 在我的情况下,这只是一个愚蠢的原因,但这些是可以占用大部分时间的错误,因为错误并不直接指向它。

在删除并重新克隆项目后,我出现了相同的lombok错误。 在做了这个线程中前面提到的步骤后,我仍然有错误,然后我发现我的SKD默认为版本11。我把它改回1.8,一切都恢复正常了。

>项目设置>项目 我将Project SDK和Project语言级别更改为1.8

PS,在这个IntelliJ版本中,mac上默认设置的位置与前面提到的不同: 文件——>新项目设置——>新项目的首选项——>构建、执行、部署——>编译器——>注释处理器——> 'check'启用注释处理

希望这对大家有所帮助