Java Lombok 注释无法在 Intellij 想法下编译

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24006937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 09:40:15  来源:igfitidea点击:

Lombok annotations do not compile under Intellij idea

javaintellij-idealombok

提问by yurnom

  1. I've installed the plugin for intellij idea(lombok-plugin-0.8.6-13).
  2. Added lombok.jar into classpath
  3. I can find getters and setters in the window of structure. And Intellij shows no error.
  4. Setting - Lombok plugin - Verified Intellij configuration for lombok, it shows that "configuration of IntelliJ seems to be ok".
  1. 我已经安装了 intellij idea 的插件(lombok-plugin-0.8.6-13)。
  2. 将 lombok.jar 添加到类路径中
  3. 我可以在结构窗口中找到 getter 和 setter。Intellij 没有显示错误。
  4. 设置 - Lombok 插件 - 验证 lombok 的 Intellij 配置,显示“IntelliJ 的配置似乎没问题”。

It seems everything is OK. But when I compile a test, errors come: can not find the methods getXXX and setXXX.

似乎一切正常。但是当我编译一个测试时,错误来了:找不到方法 getXXX 和 setXXX。

I opened the .class file with IntelliJ and find out that there is no setXXX and getXXX methods.

我用 IntelliJ 打开 .class 文件,发现没有 setXXX 和 getXXX 方法。

Could somebody tell me why?

有人能告诉我为什么吗?

  • plugin:lombok-plugin-0.8.6-13
  • lombok.jar:1.14.0
  • idea:13.0.2 for linux
  • jdk:1.7.0_21
  • 插件:lombok-plugin-0.8.6-13
  • lombok.jar:1.14.0
  • 想法:13.0.2 for linux
  • jdk:1.7.0_21

回答by mike

In order to solve the problem set:

为了解决问题集:

  • Preferences (Ctrl+ Alt+ S)
    • Build, Execution, Deployment
      • Compiler
        • Annotation Processors
          • Enable annotation processing
  • 首选项 ( Ctrl+ Alt+ S)
    • 构建、执行、部署
      • 编译器
        • 注释处理器
          • 启用注释处理

Make sure you have the Lombok plugin for IntelliJ installed!

确保你已经安装了 IntelliJ 的 Lombok 插件!

  • Preferences ->Plugins
  • Search for "Lombok Plugin"
  • Click Browse repositories...
  • Choose Lombok Plugin
  • Install
  • Restart IntelliJ
  • 首选项->插件
  • 搜索“Lombok 插件”
  • 单击浏览存储库...
  • 选择 Lombok 插件
  • 安装
  • 重启 IntelliJ

回答by Juraj Misur

If you're using Eclipse compiler with lombok, this setup finally worked for me:

如果您将 Eclipse 编译器与 lombok 一起使用,则此设置最终对我有用:

  • IDEA 14.1
  • Lombok plugin
  • ... / Compiler/ Java Compiler> Use Compiler: Eclipse
  • ... / Compiler/ Annotation Processors> Enable annotation processing: checked(default configuration)
  • ... / Compiler> Additional build process VM options: -javaagent:lombok.jar
  • 想法 14.1
  • 龙目岛插件
  • ... /编译器/ Java 编译器> 使用编译器:Eclipse
  • ... / Compiler/ Annotation Processors> Enable annotation processing:选中(默认配置)
  • ... /编译器> 附加构建过程 VM 选项:-javaagent:lombok.jar

The most important part is the last one, mine looks like following: enter image description here

最重要的部分是最后一个,我的如下所示: 在此处输入图片说明

Plugin is needed for IntelliJ editor to recognise getters and setters, javaagent is needed for eclipse compiler to compile with lombok.

IntelliJ 编辑器需要插件来识别 getter 和 setter,eclipse 编译器需要 javaagent 才能使用 lombok 进行编译。

回答by weiheng

For me, both lombok plugin and annotation processing enable needed, no else. No need to Use Eclipseand additional -javaagent:lombok.jaroptions.

对我来说,lombok 插件和注释处理都需要启用,没有别的。无需使用Eclipse和额外的-javaagent:lombok.jar选项。

  • Idea 14.1.3, build 141.1010
  • Lombok plugin[Preference->plugins->browse repositories->search 'lombok'->install and restart idea.
  • Preference ->search 'annotation'->enter annotation processor ->enable annotation processing.
  • 想法 14.1.3,构建 141.1010
  • Lombok 插件[Preference->plugins->browse repositories->search 'lombok'->install and restart idea。
  • 首选项->搜索'注释'->进入注释处理器->启用注释处理。

回答by robbin

I am unable to get this working with the javac compiler, and I get the same error.

我无法使用 javac 编译器进行此操作,并且出现相同的错误。

Error:(9, 14) java: package lombok does not exist

错误:(9, 14) java: 包 lombok 不存在

I have enabled annotation processor, and have also tried rebuilding the project, invalidate cache/restart. Doesn't help.

我启用了注释处理器,并尝试重建项目,使缓存/重启无效。没有帮助。

I did however get it to work partially with eclipse compiler. I say partial because although the build passes successfully, the editor still complains about "Cannot resolve symbol".

然而,我确实让它部分地与 eclipse 编译器一起工作。我说部分是因为虽然构建成功通过,但编辑器仍然抱怨“无法解析符号”。

Idea - 15.04 community edition Lombok - 1.16.6 Lombok plugin (https://github.com/mplushnikov/lombok-intellij-plugin) - 0.9.8 JDK - 1.8.0_51

Idea - 15.04 社区版 Lombok - 1.16.6 Lombok 插件 ( https://github.com/mplushnikov/lombok-intellij-plugin) - 0.9.8 JDK - 1.8.0_51

Update: Ok, I finally got this working. Mine was a gradle project, and lombok was configured as a custom "provided" configuration. Worked fine after adding this in build.gradle

更新:好的,我终于开始工作了。我的是一个 gradle 项目,lombok 被配置为自定义的“提供”配置。在 build.gradle 中添加它后工作正常

idea {
 module {
     scopes.PROVIDED.plus += [configurations.provided]
    }
}

So, 3 steps

所以,3步

  1. Install Lombok plugin from File->Settings->Plugins
  2. Enable Annotation Processor (javac compiler works too)
  3. Ensure that you have build.gradle or pom.xml updated for idea if you are adding lombok as a custom config.
  1. 从 File->Settings->Plugins 安装 Lombok 插件
  2. 启用注释处理器(javac 编译器也可以)
  3. 如果要将 lombok 添加为自定义配置,请确保已更新 build.gradle 或 pom.xml 以获取想法。

回答by Jan Kubovy

IDEA 2016.1:

想法 2016.1:

  • Install lombok plugin
  • Settings -> Compiler -> Annotation Processors -> Enable annotation processing: checked (default configuration)
  • Settings -> Compiler -> Annotation Processors -> Annotation Processors add "lombok.launch.AnnotationProcessorHider$AnnotationProcessor"
  • Also if you are using maven add to maven-compiler-plugin configuration -> annotationProcessors -> annotationProcessor: lombok.launch.AnnotationProcessorHider$AnnotationProcessor

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>${maven.plugin.compiler.version}</version>
      <configuration>
        <compilerVersion>${java.version}</compilerVersion>
        <source>${java.version}</source>
        <target>${java.version}</target>
        <annotationProcessors>
          <annotationProcessor>lombok.launch.AnnotationProcessorHider$AnnotationProcessor</annotationProcessor>
        </annotationProcessors>
      </configuration>
    </plugin>
    
  • 安装lombok 插件
  • Settings -> Compiler -> Annotation Processors -> Enable annotation processing:勾选(默认配置)
  • Settings -> Compiler -> Annotation Processors -> Annotation Processors 添加“lombok.launch.AnnotationProcessorHider$AnnotationProcessor”
  • 此外,如果您使用 maven 添加到 maven-compiler-plugin 配置 -> annotationProcessors -> annotationProcessor: lombok.launch.AnnotationProcessorHider$AnnotationProcessor

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>${maven.plugin.compiler.version}</version>
      <configuration>
        <compilerVersion>${java.version}</compilerVersion>
        <source>${java.version}</source>
        <target>${java.version}</target>
        <annotationProcessors>
          <annotationProcessor>lombok.launch.AnnotationProcessorHider$AnnotationProcessor</annotationProcessor>
        </annotationProcessors>
      </configuration>
    </plugin>
    

回答by kecso

On Itellij 15 CE, it's enough to just install Lombok Plugin (no additional configuration required).

在 Itellij 15 CE 上,只需安装 Lombok 插件就足够了(无需额外配置)。

回答by Nikhil Katre

Make sure these two requirements are satisfied:

确保满足以下两个要求:

  1. Enable annotation processing,

    Preferences > Build, Execution, Deployment > Compiler > Annotation Processors > Enable annotation processing

  2. Lombok plugin is installed and enabled for your project.

  1. 启用注释处理,

    Preferences > Build, Execution, Deployment > Compiler > Annotation Processors > Enable annotation processing

  2. 已为您的项目安装并启用 Lombok 插件。

回答by Sahli Ayoub

None of the advanced answers to this question resolved the problem for me.

这个问题的高级答案都没有解决我的问题。

I managed to solve the problem by adding a dependencie to lombok in the pom.xml file, i.e. :

我设法通过在 pom.xml 文件中向 lombok 添加一个依赖项来解决这个问题,即:

<dependency>
   <groupId>org.projectlombok</groupId>
   <artifactId>lombok</artifactId>
   <version>1.16.12</version>
</dependency>

I am using IntelliJ 2016.3.14 with maven-3.3.9

我正在使用 IntelliJ 2016.3.14 和 maven-3.3.9

Hope my answer will be helpful for you

希望我的回答对你有帮助

回答by SonnevilleJ

After spending far too long troubleshooting this, I found a simple workaround which ensures IntelliJ processes Lombok annotations correctly during builds.

在花了太长时间对此进行故障排除后,我找到了一个简单的解决方法,可确保 IntelliJ 在构建期间正确处理 Lombok 注释。

The gradle-lombok plugin is not necessary for this workaround. Your build.gradleonly requires the following:

此解决方法不需要 gradle-lombok 插件。您build.gradle只需要以下内容:

dependencies {
    compileOnly("org.projectlombok:lombok:1.16.18")
}

The workaround is to turn on the following IntelliJ setting:

解决方法是打开以下 IntelliJ 设置:

  1. Open IntelliJ preferences/settings.
  2. Navigate to Build, Execute, Deployment > Build Tools > Gradle > Runner
  3. Check the box labeled Delegate IDE build/run actions to gradle
  1. 打开 IntelliJ 首选项/设置。
  2. 导航 Build, Execute, Deployment > Build Tools > Gradle > Runner
  3. 选中标记的框 Delegate IDE build/run actions to gradle


Benefits of this workaround compared to other solutions on this page:


与此页面上的其他解决方案相比,此解决方法的优点:

  • No annotation processing necessary!
  • Able to use the Java compiler of your choice (no Eclipse compiler necessary)
  • No use of buggy gradle-lombok plugin (although perhaps someone else can solve this)
  • No VM options necessary
  • No hard-coded paths to lombok jar
  • 无需注释处理!
  • 能够使用您选择的 Java 编译器(不需要 Eclipse 编译器)
  • 不使用有问题的 gradle-lombok 插件(虽然也许其他人可以解决这个问题)
  • 无需 VM 选项
  • lombok jar 没有硬编码的路径

One downside is that IntelliJ will no longer use its own test runner. Instead, tests are always run through Gradle.

一个缺点是 IntelliJ 将不再使用自己的测试运行器。相反,测试总是通过 Gradle 运行。

回答by Ashish Khandelwal

If you're using Intellij on Mac, this setup finally worked for me.

如果你在 Mac 上使用 Intellij,这个设置最终对我有用。

Installations: Intellij

安装:Intellij

  1. Go to Preferences, search for Plugins.
  2. Type "Lombok" in the plugin search box. Lombok is a non-bundled plugin, so it won't show at first.
  3. Click "Browse" to search for non-bundled plugins
  4. The "Lombok Plugin" should show up. Select it.
  5. Click the green "Install" button.
  6. Click the "Restart Intellij IDEA" button.
  1. 转到首选项,搜索插件。
  2. 在插件搜索框中输入“Lombok”。Lombok 是一个非捆绑插件,所以一开始不会显示。
  3. 点击“浏览”搜索非捆绑插件
  4. “Lombok 插件”应该会出现。选择它。
  5. 单击绿色的“安装”按钮。
  6. 单击“重新启动 Intellij IDEA”按钮。

Settings:

设置:

  1. Enable Annotation processor

    • Go to Preferences -> Build, Execution,Deployment -->Preferences -> Compiler -> Annotation Processors
    • File -> Other Settings -> Default Settings -> Compiler -> Annotation Processors
  2. Check if Lombok plugin is enabled

    • IntelliJ IDEA-> Preferences -> Other Settings -> Lombok plugin -> Enable Lombok
  3. Add Lombok jar in Global Libraries and project dependencies.

    • File --> Project Structure --> Global libraries (Add lombok.jar)
  4. File --> Project Structure --> Project Settings --> Modules --> Dependencies Tab = check lombok

  5. Restart Intellij

  1. 启用注释处理器

    • 转到首选项 -> 构建、执行、部署 --> 首选项 -> 编译器 -> 注释处理器
    • 文件 -> 其他设置 -> 默认设置 -> 编译器 -> 注释处理器
  2. 检查 Lombok 插件是否启用

    • IntelliJ IDEA-> 首选项 -> 其他设置 -> Lombok 插件 -> 启用 Lombok
  3. 在全局库和项目依赖项中添加 Lombok jar。

    • 文件 --> 项目结构 --> 全局库(添加 lombok.jar)
  4. 文件 --> 项目结构 --> 项目设置 --> 模块 --> 依赖项选项卡 = 检查 lombok

  5. 重启 Intellij