简体   繁体   中英

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

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

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

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>

我已将 lombok 降级到 1.14.8 这个版本适用于 maven 构建,我还没有找到为什么 1.16 版本不起作用:(

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, eg change

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

to:

Foo.FooBuilder builder = Foo.builder(); // note >>Foo.<<FooBuilder; without static import

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>

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)

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.

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.

可能是您在 java 编译 (javac) 中使用-processor <processorclass>指定了-proc:none或明确指定了注释处理器吗?

就我而言,它是通过升级 JDK 解决的(以前是 1.8.0_66,现在是 1.8.0_92)

尝试在依赖项中为“lombok”模块指定参数。我遇到了同样的问题并通过这个解决方法解决了这个问题。

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.

If the maven build cannot find the symbol when accessing project lombok annotated methods, then we explicitly have to configure annotationProcessorPaths into maven-compiler-plugin

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven.compiler.plugin.version}</version>
            <configuration>
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                        <version>${lombok.version}</version>
                    </path>
                </annotationProcessorPaths>
            </configuration>
        </plugin>
    </plugins>
</build>

Above solution works like a charm for me!

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

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

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM