使用 Lombok 的 @Slf4j 和 Eclipse 构建:找不到符号日志
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16627751/
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
Building with Lombok's @Slf4j and Eclipse: Cannot find symbol log
提问by bashwin
I have the lombok plugin in Eclipse and enabled annotation processing in Eclipse under java compiler, but still it is unable to recognize the log statements when I use @Slf4j annotation.
我在 Eclipse 中有 lombok 插件并在 java 编译器下在 Eclipse 中启用了注释处理,但是当我使用 @Slf4j 注释时仍然无法识别日志语句。
Do we have to make any other settings?
我们是否需要进行任何其他设置?
回答by Roel Spilker
You also have to install Lombok into Eclipse.
您还必须将 Lombok 安装到 Eclipse 中。
See also this answeron how to do that or check if Lombok is installed correctly.
另请参阅有关如何执行此操作的答案或检查 Lombok 是否已正确安装。
Full Disclosure: I am one of the Project Lombokdevelopers.
完全披露:我是龙目岛项目的开发人员之一。
回答by Hervian
I got the same error even after Lombok was installed. For me the solution was to add another lombok annotation (i used @Data) to my class after which the eclipse errors went away. Perhaps this force refreshed some cache.
即使安装了 Lombok,我也遇到了同样的错误。对我来说,解决方案是在我的班级中添加另一个 lombok 注释(我使用了 @Data),之后 eclipse 错误就消失了。也许这种力量刷新了一些缓存。
Of course, I simply deleted the @Data annotation afterwards.
当然,我后来干脆删除了@Data注解。
回答by vinsinraw
I also faced the similar issue on log and @Slf4j on my STS environment. To resolve this, here is what I did on spring tool suite (sts-4.4.0.RELEASE) and lombok-1.18.10.jar (current latest version available in mavenrepository).
我在 STS 环境中的日志和 @Slf4j 上也遇到了类似的问题。为了解决这个问题,这是我在 spring 工具套件 (sts-4.4.0.RELEASE) 和 lombok-1.18.10.jar(mavenrepository 中提供的当前最新版本)上所做的。
If having maven project, ensure lombok dependency added to it. Else you need manually add the jar to your project classpath.
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.10</version> <scope>provided</scope> </dependency>
Clean build the maven application. This will download lombok jar in your .m2 location by default from maven repository. The path would be
org\projectlombok\lombok\1.18.10\
Now open command prompt and navigate to the lombok path and execute command
java -jar lombok-1.18.10.jar
C:\xxx\xxx\org\projectlombok\lombok\1.18.10>java -jar lombok-1.18.10.jar
Opens up lombok dialog box. If see message
Can't find IDE
ClickSpecify location...
Provide the path to your STS root locationMy case it is
C:\apps\sts-4.4.0.RELEASE\SpringToolSuite.exe
Install/Update
Install successful Click Quit Installer
Now in explorer navigate to your STS root path.
C:\apps\sts-4.4.0.RELEASE\
We seelombok.jar
placed in the sts root path Now edit in notepadSpringToolSuite4.ini
file We see following appended at the end-javaagent:C:\apps\sts-4.4.0.RELEASE\lombok.jar
Start STS using
SpringToolSuite4.exe
Clean, rebuild your project.
如果有 maven 项目,请确保添加了 lombok 依赖项。否则,您需要手动将 jar 添加到您的项目类路径中。
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.10</version> <scope>provided</scope> </dependency>
清理构建 Maven 应用程序。默认情况下,这将从 maven 存储库下载 lombok jar 到您的 .m2 位置。路径将是
org\projectlombok\lombok\1.18.10\
现在打开命令提示符并导航到 lombok 路径并执行命令
java -jar lombok-1.18.10.jar
C:\xxx\xxx\org\projectlombok\lombok\1.18.10>java -jar lombok-1.18.10.jar
打开 lombok 对话框。如果看到消息
Can't find IDE
单击Specify location...
提供 STS 根位置的路径我的情况是
C:\apps\sts-4.4.0.RELEASE\SpringToolSuite.exe
Install/Update
安装成功点击退出安装程序
现在在资源管理器中导航到您的 STS 根路径。
C:\apps\sts-4.4.0.RELEASE\
我们看到lombok.jar
放置在sts根路径下 现在在记事本SpringToolSuite4.ini
文件中编辑我们看到最后附加了以下内容-javaagent:C:\apps\sts-4.4.0.RELEASE\lombok.jar
使用
SpringToolSuite4.exe
Clean启动 STS ,重建您的项目。
回答by Naveen
this got the fix to me by adding the slf4j dependency, Lombok can identify the slf4j but does not get the download, this is true for java project if you are using spring boot then slf4j comes by default.
这通过添加 slf4j 依赖项解决了我的问题,Lombok 可以识别 slf4j 但无法获取下载,如果您使用的是 Spring Boot,那么对于 Java 项目来说这是正确的,那么默认情况下 slf4j 就会出现。
here are my dependencies
这是我的依赖项
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
<scope>provided</scope>
</dependency>