Backend Development 7 min read

Packaging Spring Boot Applications with Maven and Deploying via Docker

This guide explains how to use Maven plugins such as spring-boot-maven-plugin, maven-dependency-plugin, and maven-jar-plugin to package a Spring Boot application into an executable JAR with external lib dependencies, and shows how to deploy the resulting artifacts in a Docker container using a custom Dockerfile.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Packaging Spring Boot Applications with Maven and Deploying via Docker

Spring Boot is a popular Java framework that simplifies development with features like auto‑configuration. When building a Spring Boot project, Maven (or Gradle) is typically used for compilation and packaging.

This article demonstrates how to configure Maven to produce an executable JAR while separating the dependency libraries, making the artifact ready for Docker deployment.

Maven dependency plugins

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

Configuration example for the Spring Boot plugin (set to produce a ZIP layout and include JVM arguments):

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <executable>true</executable>
        <layout>ZIP</layout>
        <jvmArguments>-Dfile.encoding=UTF-8</jvmArguments>
        <!-- specify which JARs to include; use "nothing" if none -->
        <includes>
            <include>
                <groupId>nothing</groupId>
                <artifactId>nothing</artifactId>
            </include>
        </includes>
    </configuration>
</plugin>

The dependency plugin is set to copy all 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>
                <outputDirectory>${project.build.directory}/lib</outputDirectory>
                <excludeScope>provided</excludeScope>
            </configuration>
        </execution>
    </executions>
</plugin>

The maven-jar-plugin is also configured to produce a JAR whose META-INF/MANIFEST.MF contains the main class and a class‑path entry pointing to the lib/ directory:

<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 running mvn package , the output consists of an executable JAR and a separate lib directory containing all dependency JARs.

Dockerfile for deployment

The Dockerfile copies the lib directory into the container, adds the built JAR, and sets the entrypoint to run the application with specific JVM options:

# Set JDK version
FROM java:8

MAINTAINER daqi
# Set 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 built JAR and rename
ADD Esurvey-backend-1.1.1.jar springboot.jar

# Expose application port
EXPOSE 9996

# Run the JAR with memory limits and profile
ENTRYPOINT ["java","-Xms256m","-Xmx256m","-jar","/springboot.jar","--spring.profiles.active=prod","-c"]

With this Docker image, the Spring Boot service can be started quickly in any environment, while keeping the dependency libraries isolated from the executable JAR.

For further details on using docker-compose to orchestrate the deployment, refer to the linked blog post.

JavaDockerMavenSpring BootpackagingDockerfile
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

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.