Can't compile project when I'm using Lombok under IntelliJ IDEA

余生颓废 提交于 2019-11-26 03:03:50

问题


I\'m trying to use Lombok in my project that I\'m developing using IntelliJ IDEA 11.

I\'ve installed 3rd-party plugin for IDEA and it seems working fine because IDEA sees all autogenerated methods/fields.

So I have a class that uses Slf4j. I annotated it like this

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class TestClass
{
    public TestClass()
    {
        log.info(\"Hello!\");
    }
}

But when I build my project compiler spits: cannot find symbol variable log.

Could you please tell me what I\'m missing here ?

Update: It turned out it\'s RequestFactory annotation process that fails.

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

Any ideas on workarounds ?

Update2: Perhaps it\'s not something readers want to hear but I ended up switching to Scala.


回答1:


I have fixed it in IDEA 12 by setting check box Enable annotation processing in:

Settings -> Compiler -> Annotation Processors

For IDEA 2016.2:

Preferences... > Build, Execution, Deployment > Compiler > Annotation Processors

After enabling, run Build -> Rebuild Project to have annotations recognized and eliminate errors.


For IDEA 2019.2.1, depending on how the project is configured, installing the Project Lombok plugin may not be sufficient. Here is another way to use Project Lombok with IntelliJ IDEA:

  1. Visit https://projectlombok.org/download
  2. Download the JAR file into the project lib directory (e.g., $HOME/dev/java/project/libs).
  3. Start the IDE.
  4. Click File 🠖 Settings.
  5. Expand Build, Execution, Deployment 🠖 Compiler 🠖 Annotation Processors.
  6. Ensure Enable annotation processing is checked.
  7. Ensure Store generates sources relative to is selected based on the project's module settings (if Module output directory doesn't work, come back and try the other setting).
  8. Click Apply.
  9. Click Plugins.
  10. Click Marketplace.
  11. Set search field to: lombok
  12. Install Lombok.
  13. Click OK.
  14. Restart the IDE if prompted.
  15. Click File 🠖 Project Settings.
  16. Select Libraries.
  17. Click the + symbol to add a new project library (or press Alt+Insert).
  18. Select Java.
  19. Set the path to: $HOME/dev/java/project/libs/lombok.jar
  20. Click OK.
  21. Select the modules to apply.
  22. Click OK.
  23. Optionally, rename lombok to Project Lombok 1.18.8.
  24. Click OK.

The project can now import from the lombok package and use Project Lombok annotations (e.g., lombok.Setter and lombok.Getter).




回答2:


Enabling annotation processing will make it work

But if you are on a Mac, make sure you enable annotation processing(tick the checkbox) from both the places available.

1.) Intellij Idea -> Preferences -> Compiler -> Annotation Processors

2.) File -> Other Settings -> Default Settings -> Compiler -> Annotation Processors




回答3:


Picture representation of resolving this issue.

First enable annotation processors and try. This may or may not work.

Post that, you can install the lombok plugin from intellij, (After installation Intellij will restart to enable the plugin, so make sure you save your work.(Intellij does save all the changes before restart, just to be on the safe side.)) screenshot below:




回答4:


As noted here, quote: "You should activate external compiler option and enable annotation processors or disable external compiler and disable all of annotation compilers to work with lombok". This fixed my problem. Note that I added the Scala plugin prior to receiving this error, so I suspect the plugin changed some of the above settings.




回答5:


If you have checked both these steps as follows

  1. Enable annotations : this is a check done in IntelliJ preferences.
  2. Importing lombok into IntelliJ classPath (Preferences -> Plugins)

and still getting errors then please check the compiler - if it is JAVAC or ECLIPSE.

You can check the compiler in Preferences -> Build,Execution,Deployment -> Compiler -> Java Compiler.

Change the Use compiler to Javac (if it is Eclipse). This is what worked for me.




回答6:


there is a plugin for intellij. see here: https://projectlombok.org/download.html




回答7:


Just for reference using IntelliJ 2018.3, I solved this issue (using @Data annotation to insert getter/setter) following the three steps:

  1. File -> Settings -> Build, Execution, Deployment -> Annotation Processors -> Enable Annotation Processing;

Do remember to Apply the change.

  1. Install plugin lombok in the same setting dialog;

  1. It seems good enough for now, it requires to restart IntelliJ and then rebuild your project.

Best wishes :)




回答8:


Do you have lombok as dependency of your project? lombok.jar must be on the classpath during compiling of the project, which is using any of lombok-annotations.




回答9:


  1. Make sure it's added correctly to your project.

example for Gradle:

 dependencies {
     compileOnly 'org.projectlombok:lombok:1.18.8'
     annotationProcessor 'org.projectlombok:lombok:1.18.8'
     ...
 }
  1. Install Lombok plugin for your IDE
  2. Check "Enable annotation processing" checkbox in IDE (IntellijIdea), have no idea if there is anything like this for other IDEs like Eclipse.



回答10:


I was on Mac

This is my IntelliJ IDEA and Mac Version - IntelliJ IDEA 2017.1.5 Build #IU-171.4694.70 --- Mac OS X 10.12

In addition to enabling annotation processing (tick the checkbox) at these 2 places.

1.) Intellij IDEA -> Preferences -> Compiler -> Annotation Processors

.

2.) File -> Other Settings -> Default Settings -> Compiler -> Annotation Processors

I had to install Lombok plugin too to make it work.

3.) Intellij IDEA -> Preferences -> Plugins ->Browse Repositories-> Search for "Lombok"-> install plugin -> Apply and restart IDEA




回答11:


For those of you who are still having trouble:

In addition to the above steps of enabling annotation processors and installing the IntelliJ Lombok plugin, I also had to Build -> Rebuild Project.




回答12:


1、install lombok plugin for IDEA

Intellij Idea -> Preferences -> Plugins -> type in lombok -> Search in Repositories -> install -> restart IDEA

2、 config lombok plugin

Enabling annotation processing will make it work

But if you are on a Mac, make sure you enable annotation processing in the following two places:

Intellij Idea -> Preferences -> Build, Execution, Deployment -> Compiler -> Annotation Processors, check the checkbox of "Enable annotation processing". File -> Other Settings -> Default Settings -> Build, Execution, Deployment -> Compiler -> Annotation Processors, check the checkbox of "Enable annotation processing".




回答13:


It didn#t work for me with any of the above solutions. I added <scope>provided</scope> to the dependency in pom.xml and it worked.

<dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.16.20</version>
        <scope>provided</scope>
    </dependency>



回答14:


I have faced this problem after updating the IDEA to 2018.3. I had to update all the existing plugin




回答15:


I had a similar issue when building with JDK8, set the project back to JDK7 and it worked fine. Could be an older version of Lombok that won't compile under JDK8.




回答16:


I had the same problem so I will leave this here for future reference of my obliviousness.

When Alt+Entering on @Slf4j annotation I mistakenly chose invalid import. So if the code doesn't compile make sure that you import:

import lombok.extern.slf4j.Slf4j;




回答17:


FWIW - When setting compiler annotations as described above, make sure you also check for any profiles that you are using such as "Maven default annotations processors".




回答18:


It may happen that even if you have it configured properly and it is visible among the libraries and in Gradle dependencies list, IntelliJ still does not have it in class path. Or it is there, but configured with different scope (ex: test instead of compile.)

First, make sure you have plugin installed and annotation processing enabled, as stated in other answers.

If you still have annotation not recognized, place cursor on it, hit ALT+ENTER (or OPTION+ENTER) and see if you have an menu option Add library: Gragle: org.projectlombok:lombok:VERSION to class path. If you can see it, choose this one and it may solve your problem.

You may check the library and it's scope in: Project settings / Modules / Dependencies tab (search for lombok in there)




回答19:


If you tried all solutions presented here and still can't compile sources, also look here: Static import of builder class breaks bytecode generation in Maven - look at your sources if it has such static imports. This affects maven plugin, so compilation will fail on other build systems outside IntelliJ IDEA.




回答20:


In my case, I had all the things mentioned below in place and it still wasn't working.

  1. I had lombok plugin installed correctly
  2. Annotation Processors, also checked.
  3. My Java compiler was set to JAVAC

To fix my issues I had to

  1. Update lombok to the latest version (v0.15) as at 7, Oct, 2017.
  2. Restart IntelliJ.
  3. Rebuild project.

See screenshots on how to update and rebuild project below.

How to update lombok

How to rebuild project




回答21:


You can fix cannot find symbol variable log when using lombok in IntelliJ IDEA 2017.1.3 by doing this:

IntelliJ Preferences > Build, Execution, Deployment > Annotation Processors > [Check] Enable annotation processing > Apply




回答22:


Install the below plugin and restart the IDE to resolve the errors:

File -> Settings -> Plugins-> Browse Repositories -> Lombok Plugin

Enable annotation processor:

File -> Settings -> Build, Execution, Deployment -> Compiler -> Annotation Processors
File -> Other Settings -> Default Settings -> Compiler -> Annotation Processors



回答23:


I faced a similar problem. The following worked for me.

download (from here https://projectlombok.org/download) and add the jar in your project structure.

Steps: File -> Project Structure -> Global Libraries




回答24:


If lombok libs are set correctly, as many mentioned above, click on the annotation that wouldn't compile, Alt-Enter, a correct import will appear, and the code will compile. - it took me a while to figure it out. I put it here just in case people may miss the simple obvious.




回答25:


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.



来源:https://stackoverflow.com/questions/9424364/cant-compile-project-when-im-using-lombok-under-intellij-idea

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