Using Lombok Library With JDK 23
In this brief tutorial, learn how to avoid compilation errors when using the Lombok library with Java 23.
Join the DZone community and get the full member experience.
Join For FreeJava 23 is finally out, and we can start migrating our project to it. The very first pitfall comes quickly when switching to the latest JDK 23 with compilation issues when using the Lombok library in your project. Let's begin with the symptom description first.
Description
The Lombok library heavily relies on annotations. It's used for removing a lot of boilerplate code; e.g., getters, setters, toString
, loggers, etc.
@Slf4j
usage for simplified logging configuration
Maven compilation errors coming from Lombok and Java 23 look like this:
[INFO] --- [compiler:3.13.0:compile [ (default-compile) @ sat-core ---
[WARNING] Parameter 'forceJavacCompilerUse' (user property 'maven.compiler.forceJavacCompilerUse') is deprecated: Use forceLegacyJavacApi instead
[INFO] Recompiling the module because of changed source code
[INFO] Compiling 50 source files with javac [debug parameters release 23] to target\classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] spring-advanced-training\sat-core\src\main\java\com\github\aha\sat\core\aop\BeverageLogger.java:[21,2] error: cannot find symbol
symbol: variable log
location: class BeverageLogger
...
[INFO] 16 errors
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] [BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.090 s
[INFO] Finished at: 2024-09-26T08:45:59+02:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal [org.apache.maven.plugins:maven-compiler-plugin:3.13.0:compile (default-compile) on project sat-core: Compilation failure: Compilation failure:
[ERROR] spring-advanced-training\sat-core\src\main\java\com\github\aha\sat\core\aop\BeverageLogger.java:[21,2] error: cannot find symbol
[ERROR] symbol: variable log
[ERROR] location: class BeverageLogger
...
- Note: The
@Slf4j
annotation is just an example. It's demonstrated here because these are the first errors in the build logs. However, it's related to any other already mentioned Lombok annotation.
Explanation
The compilation error is caused by a change in the behavior of annotation processing in Java 23. See JDK 23 Release notes and this statement:
As of JDK 23, annotation processing is only run with some explicit configuration of annotation processing or with an explicit request to run annotation processing on the
javac
command line. This is a change in behavior from the existing default of looking to run annotation processing by searching the class path for processors without any explicit annotation processing related options needing to be present.
You can find more details about it here.
Solution
In order to be able to use Lombok with the new Java 23, we need to turn on the full compilation processing. It can be done in Maven as:
- To have the latest
maven-compiler-version
(it's version 3.13.0 at the time of writing this article) - Setup
maven.compiler.proc
property withfull
value.
<properties>
...
<java.version>23</java.version>
<maven-compiler-plugin.version>3.13.0</maven-compiler-plugin.version>
<maven.compiler.proc>full</maven.compiler.proc>
</properties>
<build>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
It's all we need to make our project compilable again.
[INFO] --- compiler:3.13.0:compile (default-compile) @ sat-core ---
[WARNING] Parameter 'forceJavacCompilerUse' (user property 'maven.compiler.forceJavacCompilerUse') is deprecated: Use forceLegacyJavacApi instead
[INFO] Recompiling the module because of changed source code.
[INFO] Compiling 50 source files with javac [debug parameters release 23] to target\classes
[INFO]
[INFO] --- resources:3.3.1:testResources (default-testResources) @ sat-core ---
[INFO] Copying 2 resources from src\test\resources to target\test-classes
Conclusion
This article has covered the issue related to using the Lombok library and upgrading to JDK 23. The complete change (but with more changes) is visible in this GitHub commit.
Opinions expressed by DZone contributors are their own.
Comments