cannot find symbol with Lombok 1.18.26 when JDK upgraded to JDK17 in multi module maven project

1.2k Views Asked by At

I was using Lombok with JDK8 and everything was working fine. However, when I tried upgrading to JDK17, compilation fails with error

error: cannot find symbol
[ERROR]   symbol:   class Builder
cannot find symbol
[ERROR]   symbol:   method getKey()

In my parent pom, I have

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

Project structure is :

MainProject
|
 -subProject1 --> pom1.xml
 -subProject2 --> pom2.xml
 -subProject3 --> pom3.xml
|
- Main pom.xml
- lombok.config

The main pom.xml has

  <modules>
    <module>subProject1</module>
    <module>subProject2</module>
    <module>subProject3</module>
  </modules>

I am getting same issue in Intellij as well as Mac terminal so this issue is not specific to Intellij.

Tried multiple things from internet. Added lombok.addLombokGeneratedAnnotation = true in lombok.config. Tried <annotationProcessorPaths> in maven-compiler plugin.But nothing is working.I am totally blocked.Kindly suggest.

2

There are 2 best solutions below

0
khmarbaise On

The configuration in maven-compiler-plugin should look like this. The property lombok.version (version:

  <properties>
    <maven.compiler.release>20</maven.compiler.release>
    <spring.boot.version>3.2.0-M2</spring.boot.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <lombok.version>1.18.28</lombok.version>
  </properties>


  <dependencyManagement>
    <dependencies>
      ...
      <dependency>
      <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>${lombok.version}</version>
      </dependency>

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

It is also important to define the lombok project as a dependency in the modules which really use it like this:

<dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
  <scope>provided</scope>
</dependency>
0
Chris S On

Folks, after migrating from Spring Boot 2.7 (JDK 8) to Spring Boot 3.1.5 (JDK17; Maven 3.9.5) I was also facing this issue: cannot find symbol

Starting a new project shows the setup is working as expected. So its project related.

Verification:

  • Java version: 17.0.9, vendor: Azul Systems, Inc.
  • Maven version: /usr/local/Cellar/maven/3.9.5
  • Spring Boot version: 3.1.5
  • Lombok version: 1.18.30
pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.db.orinoco.soa.sb</groupId>
    <artifactId>javaelevenstarter</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>javaelevenstarter</name>
    <description>Demo project for Spring Boot 3 and JDK 17 (maven 3.9.5)</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-oauth2-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>
lombok.config

# Tell the @Slf4j annotation to generate the field 'LOG'.
lombok.log.fieldName = LOG
person.java

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
class Person {

    private String name;
    private String surname;
    private String town;
}
JavaElevenStarterApplicationTests.java

@Slf4j
@SpringBootTest
class JavaElevenStarterApplicationTests {

    @Test
    void contextLoads_lombock_AllArgsConstructor_Getter() {

        final Person person = new Person("Dave", "Developer", "Davetown");
        final String name = person.getName();
        final String surname = person.getSurname();
        final String town = person.getTown();

        LOG.info("Person data - Name: {}, surname: {}, town: {}", name, surname, town);
        Assert.hasText(name, "Dave");
        Assert.hasText(surname, "Developer");
        Assert.hasText(town, "Davetown");
    }

    @Test
    void contextLoads_Builder_pattern() {

        final Person person = Person.builder()
                .name("Dave")
                .surname("Developer")
                .town("Davetown")
                .build();

        final String name = person.getName();
        final String surname = person.getSurname();
        final String town = person.getTown();

        LOG.info("Person data - Name: {}, surname: {}, town: {}", name, surname, town);
        Assert.hasText(name, "Dave");
        Assert.hasText(surname, "Developer");
        Assert.hasText(town, "Davetown");
    }
}

Tests passed: 2

console:

...JavaElevenStarterApplicationTests: Person data - Name: Dave, surname: Developer, town: Davetown

...JavaElevenStarterApplicationTests: Person data - Name: Dave, surname: Developer, town: Davetown

This leads me to the assumption some dependency are causing the issue and lombok fails first.

Inspect your project dependencies and try to get your project compile.