Skip to content

Instantly share code, notes, and snippets.

@BDF
Last active March 8, 2021 12:21
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save BDF/393fe8739b035f461ce4 to your computer and use it in GitHub Desktop.
Save BDF/393fe8739b035f461ce4 to your computer and use it in GitHub Desktop.
Using the maven shade plugin on a Spring Boot application
  • Maven shade with Spring Boot

The goal was to create an uber-jar out of the Spring boot application. Following the directions given at Apache went very smoothly yet when I tried to execute the jar a large number of spring beans were missing and not getting loaded during runtime. For example, one of the beans not getting loaded was 'AutoConfigurationPackages'

jar -tvf name.jar | grep AutoConfigurationPackages showed that the class file was present in tha jar.

jar -xvf name.jar to extract jar into the current directory. java -cp . com.pq.Main gave the same behavior as trying to run the executable jar.

Turns out the Spring jars have a number of directives ( things like mappings to XML Schema URIs to namespace handlers ) in each jar. Default behavior of shade is to overwrite those files.

Part of the solution was found on StackoverFlow. The other half was found in the Spring Boot issues

Here is what I needed to add to my pom to get things working.

<transformers>
    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
        <resource>META-INF/spring.handlers</resource>
    </transformer>
    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
        <resource>META-INF/spring.schemas</resource>
    </transformer>
    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
        <resource>META-INF/spring.factories</resource>
    </transformer>
</transformers>

This directs the maven shade plugin to append the spring.handlers from the various spring jars into one spring.handlers file in the uber-jar instead overwriting it with the latest spring jar seen. The same needed to be done with spring.schemas and spring.factories.

"missing spring boot" missing org.springframework.boot.autoconfigure.AutoConfigurationPackages

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment