Java 将 Lombok 插件添加到 IntelliJ 项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41161076/
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-11 23:12:15  来源:igfitidea点击:

Adding Lombok plugin to IntelliJ project

javaspring-bootlombokintellij-lombok-plugin

提问by Anders Pedersen

I'm trying to add Lombok to my Spring Boot project in IntelliJ IDEA. So far, I've

我正在尝试将 Lombok 添加到 IntelliJ IDEA 中的 Spring Boot 项目中。到目前为止,我已经

  1. added the plugin under Settings- Plugins(version 0.13.16)

  2. added compile('org.projectlombok:lombok')to my Gradle dependencies

  3. enabled annotation processing

  1. Settings- Plugins(version 0.13.16)下添加了插件

  2. 添加compile('org.projectlombok:lombok')到我的 Gradle 依赖项中

  3. 启用注释处理

It still doesn't recognize either the Lombok import or the annotations.

它仍然无法识别 Lombok 导入或注释。

What am I missing?

我错过了什么?

Solved:

解决了:

I had to run an update on my Gradle file.

我不得不对我的 Gradle 文件运行更新。

回答by Henrik Kirk

Be sure to activate the plugin for the project in the Intellij settings.

请务必在 Intellij 设置中激活项目的插件。

  1. Click Settings Other Settings Lombok Plugin.
  2. Ensure Enable Lombock plugin...is checked.
  1. 单击设置其他设置 Lombok 插件
  2. 确保启用 Lombok 插件...被选中。

Lombok Plugin Settings

Lombok 插件设置

回答by Aman Tuladhar

You need to Enable Annotation Processingon IntelliJ IDEA

您需要启用标注处理IntelliJ IDEA

> Settings > Build, Execution, Deployment > Compiler > Annotation Processors

> Settings > Build, Execution, Deployment > Compiler > Annotation Processors

enter image description here

在此处输入图片说明

回答by Vikash

To add the Lombok IntelliJ plugin to add lombok support IntelliJ:

添加 Lombok IntelliJ 插件以添加 lombok 支持 IntelliJ:

  • Go to File > Settings > Plugins
  • Click on Browse repositories...
  • Search for Lombok Plugin
  • Click on Install plugin
  • Restart IntelliJ IDEA
  • 转到文件 > 设置 > 插件
  • 单击浏览存储库...
  • 搜索 Lombok 插件
  • 点击安装插件
  • 重启 IntelliJ IDEA

回答by WesternGun

I just found how.

我刚刚发现如何。

I delete the first occurrence of lombok @Slf4jor logwhere the compiler complains, and wait for the warning(the red bubble) of IDEA, suggesting "add the lombok.extern.Slf4j.jar to classpath". Since then all goes well. It seems IDEA likes to complain about lombok.

我删除了lombok的第一次出现@Slf4jlog编译器抱怨的地方,并等待IDEA的警告(红色气泡),建议“将lombok.extern.Slf4j.jar添加到类路径”。从那以后一切都很顺利。看来 IDEA 喜欢抱怨 lombok。

回答by Mohammed Rafeeq

If after installing the lombok intellij pluginand enabling annotation processing, if your getter and setters are still not recognised in intellij, do check if the pluginversion is compatible with the intellijversion you use.

如果安装lombok intellij plugin并启用注解处理后,如果您的getter和setter在intellij中仍然无法识别,请检查plugin版本是否与intellij您使用的版本兼容。

It is listed under the Downloads section:

它列在“下载”部分下:

回答by Swagga Ting

For me it didn't work after doing all of the steps suggested in the question and in the top answer. Initially the import didn't work, and then when I restarted IntelliJ, I got these messages from the Gradle Plugin:

对我来说,在完成问题和最佳答案中建议的所有步骤后,它不起作用。最初导入不起作用,然后当我重新启动 IntelliJ 时,我从 Gradle 插件收到这些消息:

Gradle DSL method not found: 'annotationProcessor()'
Possible causes:<ul><li>The project 'wq-handler-service' may be using a version of the Android Gradle plug-in that does not contain the method (e.g. 'testCompile' was added in 1.1.0).
Upgrade plugin to version 2.3.2 and sync project</li><li>The project 'wq-handler-service' may be using a version of Gradle that does not contain the method.
Open Gradle wrapper file</li><li>The build file may be missing a Gradle plugin.
Apply Gradle plugin</li>

This was weird because I don't develop for Android, just using IntelliJ for Mac OS.

这很奇怪,因为我不为 Android 开发,只是为 Mac OS 使用 IntelliJ。

To be fair, my build.gradlefile had these lines in the dependenciessection, which I copied from a colleague:

公平地说,我的build.gradle文件在dependencies我从同事那里复制的部分中有这些行:

compileOnly group: 'org.projectlombok', name: 'lombok', version: '1.16.20'
annotationProcessor group: 'org.projectlombok', name: 'lombok', version: '1.16.20'

After checking versions, the only thing that completely solved my problem was adding the below to the pluginssection of build.gradle, which I found on this page:

检查版本后,唯一完全解决我的问题的是将以下内容添加到我在此页面上找到的plugins部分:build.gradle

id 'net.ltgt.apt' version '0.15'

Looks like it's a

看起来这是一个

Gradle plugin making it easier/safer to use Java annotation processors

Gradle 插件使使用 Java 注释处理器更容易/更安全

(ltgt plugin page)

ltgt 插件页面

回答by Roger Hill

There is a lot of really helpful info posted here, but there is one thing that all the posts seem to have wrong. I could not find any 'Settings' option under 'Files', and I hunted around for 10 minutes looking through all the menus until I found the settings under 'IntelliJ IDE' -> 'Preferences'.

这里发布了很多非常有用的信息,但有一件事似乎所有的帖子都有错误。我在“文件”下找不到任何“设置”选项,我在所有菜单下搜索了 10 分钟,直到找到“IntelliJ IDE”->“首选项”下的设置。

I don't know if I am using a differing OS version or IntelliJ version from other posters, or if it is because I am a stupid Windows user that doesn't know that settings == preferences on a mac (Did I miss the memo?), but I hope this helps you if you aren't finding the paths that other posts are suggesting.

我不知道我是否使用了与其他海报不同的操作系统版本或 IntelliJ 版本,或者是因为我是一个愚蠢的 Windows 用户,不知道 mac 上的设置 == 首选项(我是否错过了备忘录?),但如果您没有找到其他帖子建议的路径,我希望这对您有所帮助。

回答by EgorTitov

I had the same problem after updating IntelliJ IDE, the fix was: delete existed plugin lombok and install it again (the newest version),

更新 IntelliJ IDE 后我遇到了同样的问题,修复方法是:删除现有插件 lombok 并重新安装(最新版本),

回答by RanaIssa

I would like to add that in my case (My OS is Linux Mint and using IntelliJ IDEA). My compiler complaining about these annotations I was using: @Data @RequiredArgsConstructor, even though I had installed and activated the Lombok plugin.Install Lombok in IntelliJ Idea. I am using Maven. So I had to add this dependency in my configuration file (pom.xml file):

我想在我的情况下添加它(我的操作系统是 Linux Mint 并使用 IntelliJ IDEA)。我的编译器抱怨我使用的这些注释:@Data @RequiredArgsConstructor,即使我已经安装并激活了 Lombok 插件。在 IntelliJ Idea 中安装 Lombok。我正在使用 Maven。所以我不得不在我的配置文件(pom.xml 文件)中添加这个依赖项:

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

回答by Naveen

To install the plugin manually, try:

要手动安装插件,请尝试:

  1. DownloadLombok zip file (ensure Lombok matches the IDE version).
  2. Select Preferences Plugins Install Plugins from Disk.
  1. 下载Lombok zip 文件(确保 Lombok 与 IDE 版本匹配)。
  2. 选择Preferences Plugins 从磁盘安装插件

IntelliJ Plugin Preferences Dialog

IntelliJ 插件首选项对话框