Home > Net >  How can I tell Spring Boot to place some of the classes at the root of the jar instead of BOOT-INF?
How can I tell Spring Boot to place some of the classes at the root of the jar instead of BOOT-INF?

Time:04-27

I'm trying to set a custom log Handler in my Spring Boot (version 2.6.3) application. The result is a ClassNotFound as described in this other question

Can't override java.util.logging.LogManager in a Spring Boot web application: Getting java.lang.ClassNotFoundException on already loaded class

Based on the answer to that question, it seems I need my Handler and all its dependencies to be placed into the root of the executable jar.

Is there a direct way to accomplish this during the Maven build, i.e. not by extracting and repackaging the jar myself post-build?

CodePudding user response:

This issue is a result of BOOT-INF fat jar structure introduced by Spring Boot 1.4.

There is currently no straightforward solution, and it appears some of the Spring Boot maintainers do not agree there is a problem, so it could be a long time before the situation changes:

WORKAROUND #1

I had to do two things to get my application working again with a custom log handler. 1) use Maven Shade to package up the log handler with all its dependencies, and 2) launch the app with using the PropertiesLauncher class in the command line instead of using java -jar:

java -cp executable.jar:logger-shaded.jar -Dloader.main=mypackage.myapp org.springframework.boot.loader.PropertiesLauncher

The executable.jar, logger-shaded.jar, and mypackage.myapp are placeholders specific to my project, so adjust accordingly.

WORKAROUND #2

If the handler is loaded from code in a config class or from main() instead of being specified in the file loaded via java.util.logging.config.file, as discussed in the comments to the answer in this other question, then everything works as expected. I actually prefer this over Workaround #1 as it results in a smaller deployment, but it does require writing a few more lines of code.

  • Related