VOOZH about

URL: https://dzone.com/articles/using-lombok-library-witk-jdk-23

⇱ Using Lombok Library With JDK 23


Related

  1. DZone
  2. Coding
  3. Java
  4. Using Lombok Library With JDK 23

Using Lombok Library With JDK 23

In this brief tutorial, learn how to avoid compilation errors when using the Lombok library with Java 23.

By Oct. 01, 24 · Code Snippet
Likes
Comment
Save
18.2K Views

Join the DZone community and get the full member experience.

Join For Free

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

Plain Text
[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:

  1. To have the latest maven-compiler-version (it's version 3.13.0 at the time of writing this article)
  2. Setup maven.compiler.proc property with full value.
XML
<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.

Plain Text
[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.

Apache Maven Compilation error Java Development Kit Library Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Java 23: What Developers Need to Know
  • A Maven Story
  • A Spring Boot App With Half the Startup Time
  • Alternative Structured Concurrency

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

Let's be friends: