javamavenlombok

Maven + Lombok @Slf4j on JDK 21: “cannot find symbol: variable log” even though annotation processing is configured


I’m building a Spring Boot app with Maven from the CLI (no IDE build). Many classes use Lombok’s @Slf4j, but the build fails like this:

[ERROR] .../src/main/java/com/guardxinc/ui/client/CompanyDataClient.java:[57,17] cannot find symbol
[ERROR]   symbol:   variable log

It happens for multiple classes annotated with @Slf4j.

Environment

What I see in -X logs

Lombok is on the processor path:

-processor lombok.launch.AnnotationProcessorHider$AnnotationProcessor
-processorpath .../org/projectlombok/lombok/1.18.38/lombok-1.18.38.jar

And Maven prints:

(f) annotationProcessorPaths = [org.projectlombok:lombok:1.18.38.jar]
(f) annotationProcessors = [lombok.launch.AnnotationProcessorHider$AnnotationProcessor]

Yet log is not generated.

What I tried:

Minimal code causing the error

package com.example;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class CompanyDataClient {
  public void call() {
    log.info("hello"); // <-- "cannot find symbol: variable log"
  }
}

Minimal pom.xml (only parts relevant to Lombok & compilation)

<project xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>lombok-slf4j-repro</artifactId>
  <version>1.0-SNAPSHOT</version>

  <properties>
    <maven.compiler.release>21</maven.compiler.release>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <lombok.version>1.18.38</lombok.version>
    <slf4j.version>2.0.17</slf4j.version>
  </properties>

  <dependencies>
    <!-- Lombok: compile-time only + annotation processor -->
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>${lombok.version}</version>
      <scope>provided</scope>
    </dependency>

    <!-- Logging API (binding chosen at runtime; not relevant for compile error) -->
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>${slf4j.version}</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <!-- Standard annotation-processing route -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.13.0</version>
        <configuration>
          <fork>true</fork>
          <release>${maven.compiler.release}</release>

          <!-- put Lombok on the APT path -->
          <annotationProcessorPaths>
            <path>
              <groupId>org.projectlombok</groupId>
              <artifactId>lombok</artifactId>
              <version>${lombok.version}</version>
            </path>
          </annotationProcessorPaths>

          <!-- JDK 21: allow Lombok to access javac internals (defensive) -->
          <compilerArgs>
            <arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED</arg>
            <arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED</arg>
            <arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED</arg>
            <arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED</arg>
            <arg>-J--add-opens=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED</arg>
          </compilerArgs>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

Question Given the above, what else could make Maven compile the original sources without Lombok actually generating the log field, even though the processor is clearly on the path? Are there known pitfalls where another plugin/profile/source root interferes so Lombok runs for some files (or appears configured) but @Slf4j fields are still missing? Any pointers on what to look for in -X output (e.g., unexpected extra source roots, profiles re-adding src/main/java, etc.) would be much appreciated.


Solution

  • Try running

    mvn help:effective-pom
    

    Is the maven-compiler-plugin listed multiple times in the effective pom? Perhaps one is overriding the other? Is annotationProcessorPaths correct in the effective pom?

    Try running

    mvn clean compile -X -Dmaven.compiler.showDeprecation=true -Dmaven.compiler.showWarnings=true
    

    And look for output like

    [DEBUG] Using compiler 'javac' with classpath ...
    [DEBUG] Using annotation processor org.projectlombok.launch.AnnotationProcessorHider$AnnotationProcessor