Backend Development 8 min read

Packaging Spring Boot Applications with Maven and Docker: Separate JAR and Dependency Libraries

This article explains how to use Maven plugins to build a Spring Boot project into an executable JAR, separate its dependencies into a lib folder, and create a Docker image that copies those libraries, enabling clean deployment of Java backend services.

Architect
Architect
Architect
Packaging Spring Boot Applications with Maven and Docker: Separate JAR and Dependency Libraries

Spring Boot is a widely used Java framework that provides auto‑configuration and rapid development capabilities.

When developing with Spring Boot, Maven (or Gradle) is typically used for project building. This guide shows how to configure Maven to package a Spring Boot application as an executable JAR while extracting its dependencies into a separate lib directory for Docker deployment.

Maven Dependency Plugins

The spring-boot-maven-plugin packages the application as an executable ZIP archive, and the maven-dependency-plugin copies project dependencies to a specified location.

Configuration example for spring-boot-maven-plugin :

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <executable>true</executable>
        <layout>ZIP</layout>
        <!-- Resolve Windows console Chinese garble -->
        <jvmArguments>-Dfile.encoding=UTF-8</jvmArguments>
        <!-- Include specific JARs if needed; use "nothing" when no extra JARs are required -->
        <includes>
            <include>
                <groupId>nothing</groupId>
                <artifactId>nothing</artifactId>
            </include>
        </includes>
    </configuration>
</plugin>

The maven-dependency-plugin is set to copy dependencies to ${project.build.directory}/lib :

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>copy</id>
            <phase>package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <!-- Destination directory for copied dependencies -->
                <outputDirectory>${project.build.directory}/lib</outputDirectory>
                <excludeScope>provided</excludeScope>
            </configuration>
        </execution>
    </executions>
</plugin>

JAR Packaging

The maven-jar-plugin creates the JAR file and adds a MANIFEST.MF that specifies the main class and class‑path prefix lib/ so the runtime can locate the external libraries.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <mainClass>top.teainn.project.MyApplication</mainClass>
                <addClasspath>true</addClasspath>
                <classpathPrefix>lib/</classpathPrefix>
            </manifest>
        </archive>
    </configuration>
</plugin>

After the build, the target directory contains the executable JAR and a separate lib folder with all dependency JARs.

Dockerfile

The Dockerfile copies the lib directory into the container and adds the application JAR. It also sets the Java version, timezone, and entrypoint options.

# Set JDK version
FROM java:8

# Author information
MAINTAINER daqi
# Set container timezone
RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo "Asia/Shanghai" > /etc/timezone

# Copy dependencies
COPY ${OUT_DIRECTORY}/lib /lib

# Add the application JAR and rename it
ADD Esurvey-backend-1.1.1.jar springboot.jar

# Expose service port
EXPOSE 9996

# Set JVM memory limits and start the application
ENTRYPOINT ["java","-Xms256m","-Xmx256m","-jar","/springboot.jar","--spring.profiles.active=prod","-c"]

Using this Dockerfile together with docker‑compose enables straightforward deployment of the Spring Boot service inside a container.

For further details, refer to the linked blog post in the original article.

JavaDockerBackend DevelopmentmavenSpring Bootpackaging
Architect
Written by

Architect

Professional architect sharing high‑quality architecture insights. Topics include high‑availability, high‑performance, high‑stability architectures, big data, machine learning, Java, system and distributed architecture, AI, and practical large‑scale architecture case studies. Open to ideas‑driven architects who enjoy sharing and learning.

0 followers
Reader feedback

How this landed with the community

login 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.