Utilizing Lombok Library With JDK 23 – DZone – Uplaza

Java 23 is lastly out, and we will begin migrating our undertaking to it. The very first pitfall comes shortly when switching to the newest JDK 23 with compilation points when utilizing the Lombok library in your undertaking. Let’s start with the symptom description first.

Description

The Lombok library closely depends on annotations. It is used for eradicating plenty of boilerplate code; e.g., getters, setters, toString, loggers, and so forth.

@Slf4j utilization for simplified logging configuration

Maven compilation errors coming from Lombok and Java 23 appear like this:

[INFO] --- [compiler:3.13.0:compile [ (default-compile) @ sat-core ---
[WARNING]  Parameter 'forceJavacCompilerUse' (consumer property 'maven.compiler.forceJavacCompilerUse') is deprecated: Use forceLegacyJavacApi as a substitute
[INFO] Recompiling the module due to modified supply code
[INFO] Compiling 50 supply recordsdata with javac [debug parameters release 23] to targetclasses
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] spring-advanced-trainingsat-coresrcmainjavacomgithubahasatcoreaopBeverageLogger.java:[21,2] error: can't discover image
  image:   variable log
  location: class BeverageLogger
...
[INFO] 16 errors 
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] [BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Whole time:  3.090 s
[INFO] Completed at: 2024-09-26T08:45:59+02:00
[INFO] ------------------------------------------------------------------------
[ERROR] Did not execute aim [org.apache.maven.plugins:maven-compiler-plugin:3.13.0:compile (default-compile) on project sat-core: Compilation failure: Compilation failure: 
[ERROR] spring-advanced-trainingsat-coresrcmainjavacomgithubahasatcoreaopBeverageLogger.java:[21,2] error: can't discover image
[ERROR]   image:   variable log
[ERROR]   location: class BeverageLogger
...
  • Notice: The @Slf4j annotation is simply an instance. It is demonstrated right here as a result of these are the primary errors within the construct logs. Nonetheless, it is associated to some other already talked about Lombok annotation.

Rationalization

The compilation error is brought on by a change within the conduct of annotation processing in Java 23. See JDK 23 Launch notes and this assertion:

As of JDK 23, annotation processing is simply run with some express configuration of annotation processing or with an express request to run annotation processing on the javac command line. This can be a change in conduct from the prevailing default of seeking to run annotation processing by looking out the category path for processors with out any express annotation processing associated choices needing to be current.

You will discover extra particulars about it right here.

Answer

So as to have the ability to use Lombok with the brand new Java 23, we have to activate the complete compilation processing. It may be accomplished in Maven as:

  1. To have the newest maven-compiler-version (it is model 3.13.0 on the time of writing this text)
  2. Setup maven.compiler.proc property with full worth.

	...
	23
	3.13.0
	full


	
		...
		
			org.apache.maven.plugins
			maven-compiler-plugin
			${maven-compiler-plugin.model}
			
				${java.model}
				${java.model}
			
		
	

It is all we have to make our undertaking compilable once more.

[INFO] --- compiler:3.13.0:compile (default-compile) @ sat-core ---
[WARNING]  Parameter 'forceJavacCompilerUse' (consumer property 'maven.compiler.forceJavacCompilerUse') is deprecated: Use forceLegacyJavacApi as a substitute
[INFO] Recompiling the module due to modified supply code.
[INFO] Compiling 50 supply recordsdata with javac [debug parameters release 23] to targetclasses
[INFO] 
[INFO] --- sources:3.3.1:testResources (default-testResources) @ sat-core ---
[INFO] Copying 2 sources from srctestresources to targettest-classes

Conclusion

This text has coated the problem associated to utilizing the Lombok library and upgrading to JDK 23. The entire change (however with extra modifications) is seen on this GitHub commit.

Share This Article
Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Exit mobile version