41

I'm using project lombok for the first time and I have problems compiling the project via maven when I run the build I receive errors where methods annotated with project lombok annotations are called.

This is the annotated parameter:

    private @Getter @Setter String paymentNonce = null;

and in this line for example the maven breaks the build:

if (!StringUtilities.isNullOrEmpty(getPaymentNonce())) {

this is my maven dependency

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

the maven error:

[INFO] Compiling 158 source files to C:\java\repos\luna\cloudflow\cloudflow-ejb\target\classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] \java\repos\luna\cloudflow\cloudflow-ejb\src\main\java\si\arctur\controller\PaymentProcessor.java:[94,38] error: cannot find symbol
[ERROR] \java\repos\luna\cloudflow\cloudflow-ejb\src\main\java\si\arctur\controller\PaymentProcessor.java:[97,106] error: cannot find symbol
[ERROR] \java\repos\luna\cloudflow\cloudflow-ejb\src\main\java\si\arctur\controller\PaymentProcessor.java:[142,2] error: cannot find symbol
[ERROR] \java\repos\luna\cloudflow\cloudflow-ejb\src\main\java\si\arctur\controller\ShoppingCart.java:[27,6] error: cannot find symbol
[ERROR] \java\repos\luna\cloudflow\cloudflow-ejb\src\main\java\si\arctur\controller\ShoppingCart.java:[32,75] error: cannot find symbol
.....

I'm using java 8

Roel Spilker
  • 32,258
  • 10
  • 68
  • 58
simonC
  • 4,101
  • 10
  • 50
  • 78

17 Answers17

37

Had the same problem using maven-compiler-plugin v.2.3.2 After updating the version up to 3.5 problem disappeared

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.5</version>
    <configuration>
        ...
    </configuration>
</plugin>

Hope this helps

Andrew Onischenko
  • 421
  • 1
  • 4
  • 3
  • 2
    This solution is still valid with `maven-compiler-plugin:3.5.1`. – Stephan Jun 01 '16 at 08:36
  • 2
    I've tried version `maven-compiler-plugin:3.5.1` and `maven-compiler-plugin:3.7.0` but only `maven-compiler-plugin:3.5` would work with Lombok for me – Fabian May 30 '18 at 01:35
16

I was actually able to solve this by following an answer posted here :

MapStruct and Lombok not working together

Basically I had to add lombok to the maven-compiler-plugin <annotationProcessorPaths>

Kariem
  • 4,398
  • 3
  • 44
  • 73
Fabian
  • 3,310
  • 3
  • 26
  • 35
12

I have downgraded the lombok to 1.14.8 this version works with maven build, I havent found why the 1.16 verson is not working :(

simonC
  • 4,101
  • 10
  • 50
  • 78
  • Experienced the same with maven 3.0.5, Netbeans 8.0.2, java 1.8.0_72-internal; OpenJDK 64-Bit Server VM 25.72-b05 and downgrading to 1.14.8 made maven compile correctly. I've managed to compile a few time correctly with 1.16 but after a while it started to throw `error: cannot find symbol` – joao cenoura Jan 26 '16 at 16:24
  • Just see my answer, and it will be fixed in lombok's next version. – liudongmiao Jun 29 '16 at 02:23
  • This solved my issue! In my case Getters and Setters were not generated in Android Studio. – Artur Szymański Jul 11 '16 at 19:14
8

If you're using Lombok related static methods (mainly @Builder) with static imports, you might experience similar issues (even in other parts of your code!).

There is an open issue about it: https://github.com/rzwitserloot/lombok/issues/979

The current workaround is to simply not use static imports, e.g. change

import static my.org.Foo.FooBuilder
 ...
FooBuilder builder = Foo.builder();

to:

Foo.FooBuilder builder = Foo.builder(); // note >>Foo.<<FooBuilder; without static import
Gilad Bar Orion
  • 543
  • 6
  • 14
  • 1
    +1 This solved my problem. There is another workaround mentioned in the comments that I use now: "A work around is to create a static method that returns the builder in the class and use that one instead of the lombok generated one." – YingYang Nov 13 '17 at 10:41
  • Experienced this with lombok v1.18.24, so static import of @Builder's issue is still relevant – Not A Number Jan 16 '23 at 06:27
7

For other users using JDK9 and above you should add annotationProcessorPaths to maven compiler plugin

<annotationProcessorPaths>
    <path>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.16</version>
    </path>
</annotationProcessorPaths>
Abderrazzak Nejeoui
  • 1,496
  • 12
  • 9
6

My solution was to prefix the annotation with lombok package name.

@lombok.Builder
@lombok.experimental.Accessors(prefix = "m", chain = true)

rather than

@Builder
@Accessors(prefix = "m", chain = true)
B. Stackhouse
  • 477
  • 5
  • 15
3

In short, upgrade maven-compiler-plugin to up 2.4, or downgrade lombok to below 1.14.*.

It seems that maven-compiler-plugin below 2.4 doesn't support javax.annotation.processing.Processor with a name with $.

Update: You can configuration maven-compiler-plugin to fork, or update plexus-compiler-javac to 1.8.6. (maven-compiler-plugin 2.3.2 requires 1.8.1, and 2.4 requires 1.8.6)

Since 1.16, lombok uses ShadowClassLoader, which prevents the IDE promotion for lombok's internal class. However, it doesn't use the ShadowClassLoader if the classloader is org.codehaus.plexus.compiler.javac.IsolatedClassLoader. (I don't known why lombok guys use hard code to solve other issue may related with plexus-compiler-javac.)

maven-compiler-plugin 2.4, or rather, plexus-compiler-javac 1.8.6, doesn't use org.codehaus.plexus.compiler.javac.IsolatedClassLoader, so it works again.

liudongmiao
  • 455
  • 4
  • 7
  • 2
    A [pull request to solve this](https://github.com/rzwitserloot/lombok/pull/1138) has been merged and will be in lombok 1.16.9+. A [preview jar is available](https://projectlombok.org/download-edge.html) so please give it a try and give feedback. – Roel Spilker Jun 29 '16 at 07:43
2

If you are using lombok annotations in static classes in that case you will have to mention the full name of the class ie. instead of @Data to @lombok.Data . This has worked for me.

vishal
  • 29
  • 2
1

Could it be that you've specified -proc:none or explicitly specified annotation processors using -processor <processorclass> in your java compilation (javac)?

Roel Spilker
  • 32,258
  • 10
  • 68
  • 58
0

In my case it was solved by upgrading JDK (was 1.8.0_66, now 1.8.0_92)

Victor Basso
  • 5,556
  • 5
  • 42
  • 60
0

try to specify parameter for "lombok" module within dependencies.I faced the same issue and resolved this by this work around.

0x52616A657368
  • 388
  • 3
  • 24
0

I don't know but for some reason it solves my problem.

I have two classes that use @Builder to generate a build method. But one is normal, the other one is abnormal. I have checked everything and it seems ok. But when I run mvn to compile my project, the error is as follows:

Can't not find the symbol method builder()

import lombok.*;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
class A {

}


import lombok.*;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
class B {

}

Class A compiles correctly, but Class B reports the problem above.

I tried to replace the version of the Lombok JAR, but even though I set the version to latest, it's not ok.

So, I tried to import Lombok per class that I reference.

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
class B {

}

It works! It seems a bug.

Stephen S
  • 3,936
  • 2
  • 23
  • 33
朱正欢
  • 31
  • 3
0

For me the solution was, to do not use import static on lombok builder. Changing:

import static Foo.builder;

builder().id(1).build()

To:

Foo.builder().id(1).build()
Daniel Hári
  • 7,254
  • 5
  • 39
  • 54
0

In my case i have got this error because a compile error in other class but the lombok was suppressing the real problem. I recommend you to take a look in your classes to find some error. I hope it helps you

0

In my case, I fixed two config and it works.

  1. someone set compiler args to -proc:none in compiler args:

     <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-compiler-plugin</artifactId>
         <version>${maven_compiler_version}</version>
         <configuration>
             <compilerArgs>
                 <!--remove below compiler arg fixed my problem-->
                 <compilerArg>-proc:none</compilerArg>
                 <compilerArg>-parameters</compilerArg>
             </compilerArgs>
             <fork>true</fork>
             <source>${java_source_version}</source>
             <target>${java_target_version}</target>
             <encoding>${file_encoding}</encoding>
         </configuration>
     </plugin>
    
  2. Build, Execution, Deployment > Compiler > Java Compiler > Use compiler is Ajc.

I use lombok 1.18.24 and java 8.

After remove -proc:none compiler argument and change compiler to javac, the project could be compiled in IDEA maven or in standalone maven.

Xiao
  • 12,235
  • 2
  • 29
  • 36
0

I designed the <annotationProcessor> in maven-compiler-plugin

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.8.1</version>
      <configuration>
        <annotationProcessors>
          <annotationProcessor>lombok.launch.AnnotationProcessorHider$AnnotationProcessor</annotationProcessor>
        </annotationProcessors>
      </configuration>
    </plugin>
  </plugins>
</build>
0

If none of the other (common) answers worked for you::

(this is an Rare case thats Unlikely to fit yours)

  1. Inside my project, I had a java file called Comment.java,
    However, the inside of it is just BLANK
    (or only comments, no public class Comments, no package declaration).
  2. Run mvn install (using right click on the project in Eclipse)
  3. -> Fail
    eg::
    [ERROR] /G:/XXX/XXX/XXX/src/main/java/XXXX/SomeClassAA.java:[37,28] cannot find symbol
    symbol:   method add(SomeClassBB)
    
  4. -> To solve this, dont use a Blank java class in your project.

---

  • (Also, note, sometimes you need to run mvn install twice,
    the 1st run fails for no reason, but 2nd run success.
    Idk why (maybe doing mvn clean 1st time). & Probably not be your case.)
Nor.Z
  • 555
  • 1
  • 5
  • 13