Utilizing Lombok Library With JDK 23 – DZone – Uplaza

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

Description

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

@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 not discover image
  image:   variable log
  location: class BeverageLogger
...
[INFO] 16 errors 
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] [BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Complete time:  3.090 s
[INFO] Completed at: 2024-09-26T08:45:59+02:00
[INFO] ------------------------------------------------------------------------
[ERROR] Didn't execute purpose [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 not discover image
[ERROR]   image:   variable log
[ERROR]   location: class BeverageLogger
...
  • Observe: 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. Nevertheless, it is associated to every other already talked about Lombok annotation.

Clarification

The compilation error is attributable to a change within the habits of annotation processing in Java 23. See JDK 23 Launch notes and this assertion:

As of JDK 23, annotation processing is barely run with some specific configuration of annotation processing or with an specific request to run annotation processing on the javac command line. This can be a change in habits from the prevailing default of trying to run annotation processing by looking out the category path for processors with out any specific 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 total compilation processing. It may be finished in Maven as:

  1. To have the most recent 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 challenge 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 lined the difficulty associated to utilizing the Lombok library and upgrading to JDK 23. The whole 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