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.

macrozheng
macrozheng
macrozheng
How to Package Spring Boot Apps with External Dependencies Using Maven

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 layout to ZIP and use the includes element 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.xml we use include and exclude patterns to copy the required dependencies into a libs directory.

Run mvn clean package to generate the installation package in target.

After unpacking, the libs folder contains all external jars.

Startup Method Analysis

The generated jar contains the class org.springframework.boot.loader.Launcher, whose PropertiesLauncher subclass is used when the layout is ZIP. By setting loader.path (or the LOADER_PATH environment 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>.jar

Conclusion

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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Backendmavenpackagingspring-bootpropertieslauncherexternal-dependencies
macrozheng
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.