Maven build cannot find symbol when accessing project lombok annotated methods,

梦想与她 提交于 2019-12-18 13:54:22

问题


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


回答1:


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




回答2:


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 :(




回答3:


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

MapStruct and Lombok not working togather

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




回答4:


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



回答5:


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.




回答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)



回答7:


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.




回答8:


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




回答9:


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




回答10:


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




回答11:


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.



来源:https://stackoverflow.com/questions/34358689/maven-build-cannot-find-symbol-when-accessing-project-lombok-annotated-methods

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