How to Package Spring Boot Apps with External Dependencies Using Maven
This article explains how to package a Spring Boot application so that its dependencies are stored externally and loaded at runtime using Maven plugins and the PropertiesLauncher, enabling easy replacement of individual libraries without rebuilding the entire fat jar.
Introduction
Spring Boot normally builds a fat jar that contains all dependencies and can be started with
java -jar xxx.jar. In some projects the dependencies are placed outside the jar and the application is launched with
java -Dloader.path=libs -jar xxx.jar, which makes it easy to replace a single library.
Packaging Method Details
spring-boot-maven-plugin
The official Spring Boot Maven plugin creates the executable jar. To avoid bundling dependencies we set the
layoutto
ZIPand use the
includeselement to specify a non‑existent jar, effectively excluding all libraries from the fat jar.
maven-assembly-plugin
This plugin allows flexible assembly of the package. In
assembly.xmlwe use
includeand
excludepatterns to copy the required dependencies into a
libsdirectory.
Run
mvn clean packageto generate the installation package in
target.
After unpacking, the
libsfolder contains all external jars.
Startup Method Analysis
The generated jar contains the class
org.springframework.boot.loader.Launcher, whose
PropertiesLaunchersubclass is used when the layout is
ZIP. By setting
loader.path(or the
LOADER_PATHenvironment variable) we can add additional classpath locations such as
lib,${HOME}/app/lib. The command to start the application becomes:
java -jar -Dloader.path=xx1,xx2,public <jarName>.jarConclusion
This packaging and launch approach is useful for large modular projects because it allows urgent bug fixes by swapping individual dependency jars without rebuilding the whole application.
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.