Building with Lombok's @Slf4j and Intellij: Cannot find symbol log

时间秒杀一切 提交于 2019-11-28 16:56:24
Alexander Zagniotov

In addition to having Lombok plugin installed, also make sure that the "Enable annotation processing" checkbox is ticked under:

Preferences > Compiler > Annotation Processors

Note: starting with IntelliJ 2017, the "Enable Annotation Processing" checkbox has moved to:

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

Presumably, that's the Lombok @Slf4j annotation you're using. You'll need to install the Lombok plugin in IntelliJ if you want IntelliJ to recognize Lombok annotations. Otherwise, what do you expect if you try to use a field that doesn't exist?

In Intellij version 2016, 2017, enable Preferences -> Compiler -> Annotation Processors does not work for me!

The following additional checkbox helps:

I might be ungraving a dead topic but a simple solution is to check in your dependencies (Maven's pom for exemple) if you are including logback-core and logback-classic.

Slf4j is just the interface, you need the concrete implementation behind it to work.

I've been tricked twice with IDEA messing it up, now I'm good to go :D

In IDEA 13 this seems to no longer be an issue, you just have to have the Lombok plugin installed.

I've just installed the latest idea verion 2108.1 and found this issue, after installed lombok plugin and restart the Idea resolve it.

2019:

Get a plugin and you are sorted...

File > Settings > Plugins

This worked for me : File -> Settings -> Build, Execution, Deployment -> Compiler -> Annotation Processor

Tick on 'enable annotation processing'. Apply

Close

I was seeing this issue with an older version of Lombok when compiling under JDK8. Setting the project back to JDK7 made the issue go away.

This won't have been OP's problem, but for anyone else who tries everything with no success:

I had similar symptoms. Whenever I built after a mvn clean, it wouldn't find log, or getXYZ(), or builder(), or anything.

[ERROR]   symbol:   variable log
[ERROR]   location: class com.example.MyClass
[ERROR] /Path/To/Some/Java/src/main/com/example/MyClass.java:[30,38] cannot find symbol
[ERROR]   symbol:   method builder()
[ERROR]   location: class com.example.MyClass

After reading every answer I could find about QueryDSL/JPA/Hibernate/Lombok/IntelliJ/Maven issues to no avail, I worked out that the culprit was a single static import of a @Getter method that was annotated on a static field.

Spring 1.15.14.RELEASE, Intellij 2019.1.1

@SpringBootApplication
public class BarApplication implements ApplicationContextAware {
  @Getter
  private static ApplicationContext applicationContext;

  // ... start Spring application, and grab hold of ApplicationContext as it comes past
}
import ...
import static BarApplication.getApplicationContext;

@Slf4j
public class IMakeItAllFail {
   public IMakeItAllFail() {
      log.info("{}", getApplicationContext());
   }
}
@Slf4j
public class Foo {
  Foo() {
    log.info("I fail to compile but I have nothing to do with the other classes!");
  }
}

After enabling annotation processors and installing the lombok plugin, it still didn't work. We worked around it by checking the Idea option "Delegate IDE build to gradle"

Worked for me!!! It was failing on CircleCI & on Jenkins as well.

If you're a Gradle User try add the following into your dependencies:

dependencies {
    //Other Dependencies >>

    //LOMBOK Dependencies
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
    testAnnotationProcessor 'org.projectlombok:lombok'
    testCompile 'org.projectlombok:lombok'
    testImplementation 'org.projectlombok:lombok'
}

What sorted out things for me was to tick the checkbox "Use plugin registry" in Maven settings.

The path is: File -> Preferences -> Build, Execution, Deployment -> Build Tools -> Maven

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!