我试图在我的项目中使用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。


当前回答

正如这里所指出的,引用:“您应该激活外部编译器选项并启用注释处理器,或者禁用外部编译器并禁用所有注释编译器以使用lombok”。这解决了我的问题。注意,我在收到这个错误之前添加了Scala插件,所以我怀疑插件更改了上面的一些设置。

其他回答

To get this working, simply install the "Lombok Plugin" for IntelliJ. You don't need to do anything with enabling "Annotation Processors", as some other commentors have suggested. (I've tested this with the latest version of IntelliJ IDEA, currently 2017.1.2). To install the plugin, go to Settings, then Plugins, then click the "Browse repositories" button, then search for "Lombok", and install the Lombok Plugin. You will be prompted to restart IntelliJ. Afterwards, you should be able to compile from IntelliJ, and you won't receive any more error inspections.

可能发生的情况是,即使你正确地配置了它,并且它在库和Gradle依赖项列表中可见,IntelliJ仍然没有在类路径中显示它。或者它确实存在,但是配置了不同的作用域(例如:test而不是compile)。

首先,确保安装了插件并启用了注释处理,如其他答案中所述。

如果你仍然无法识别注释,将光标放在上面,点击ALT+ENTER(或OPTION+ENTER),看看是否有菜单选项Add library: Gradle: org。projectlombok:lombok:VERSION到类的路径。如果你能看到它,选择这个,它可能会解决你的问题。

你可以查看库,它的范围在: 项目设置/模块/依赖选项卡(在那里搜索lombok)

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

解决此问题的图片表示。

首先启用注释处理器并尝试。这可能有用,也可能没用。

发布后,您可以从intellij安装lombok插件(安装后intellij将重新启动以启用插件,因此请确保保存您的工作。(Intellij会在重启前保存所有更改,只是为了安全起见。))

我尝试启用lambok,重新启动intellij等,但下面对我有用。

Intellij Preferences ->Compiler -> Shared Build process VM Options,设置为

  -Djps.track.ap.dependencies=false

比跑

mvn clean install