Backend Development 13 min read

Optimizing Spring Boot Deployment: Separating Dependencies and Configurations with Maven Plugins

This article explains how to streamline Spring Boot production releases by extracting libraries and configuration files from the main JAR, using various Maven plugins such as spring-boot-maven-plugin, maven-dependency-plugin, maven-assembly-plugin, and maven-jar-plugin to create lightweight, fast‑deployable packages.

Code Ape Tech Column
Code Ape Tech Column
Code Ape Tech Column
Optimizing Spring Boot Deployment: Separating Dependencies and Configurations with Maven Plugins

Deploying Spring Boot projects in production can be cumbersome because any code change forces a full re‑packaging of a large, monolithic JAR that includes all dependencies and configuration files, leading to long upload times and inefficient updates.

Method 1: spring-boot-maven-plugin

1. Exclude configuration files and package them into a config directory

<resources>
  <resource>
    <directory>src/main/resources</directory>
    <filtering>true</filtering>
    <targetPath>${project.build.directory}/config</targetPath>
    <includes>
      <include>**/*.properties</include>
      <include>**/*.yml</include>
      <include>**/*.xml</include>
      <include>mapper/*.xml</include>
    </includes>
  </resource>
</resources>

2. Package the executable JAR while excluding dependency JARs

<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  <configuration>
    <jvmArguments>-Dfile.encoding=UTF-8</jvmArguments>
    <layout>ZIP</layout>
    <includes>
      <include>
        <groupId>no-exists-jar</groupId>
        <artifactId>non-exists-jar</artifactId>
      </include>
    </includes>
  </configuration>
  <executions>
    <execution>
      <goals>
        <goal>repackage</goal>
      </goals>
    </execution>
  </executions>
</plugin>

3. Use maven-dependency-plugin to copy dependencies into a lib directory

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-dependency-plugin</artifactId>
  <executions>
    <execution>
      <id>copy-dependencies</id>
      <phase>package</phase>
      <goals>
        <goal>copy-dependencies</goal>
      </goals>
      <configuration>
        <outputDirectory>${project.build.directory}/lib</outputDirectory>
        <excludeTransitive>false</excludeTransitive>
        <stripVersion>false</stripVersion>
        <includeScope>runtime</includeScope>
      </configuration>
    </execution>
  </executions>
</plugin>

After packaging, the directory structure contains separate config and lib folders. The application can be started with:

java -jar -Dloader.path=./lib -jar xxx.jar
Spring Boot automatically loads configuration files from the config directory, so specifying -Dspring.config.location is unnecessary.

Method 2: Maven JAR Plugin

1. Use maven-resources-plugin to copy configuration files to config

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-resources-plugin</artifactId>
  <configuration>
    <encoding>UTF-8</encoding>
  </configuration>
  <executions>
    <execution>
      <id>copy-dependencies</id>
      <phase>package</phase>
      <goals>
        <goal>copy-resources</goal>
      </goals>
      <configuration>
        <outputDirectory>${project.build.directory}/twin-web/config</outputDirectory>
        <resources>
          <resource>
            <directory>src/main/resources</directory>
          </resource>
        </resources>
      </configuration>
    </execution>
  </executions>
</plugin>

2. Use maven-jar-plugin to create an executable JAR while excluding configuration files

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  <configuration>
    <outputDirectory>${project.build.directory}/twin-web/</outputDirectory>
    <archive>
      <addMavenDescriptor>false</addMavenDescriptor>
      <manifest>
        <addClasspath>true</addClasspath>
        <classpathPrefix>./libs/</classpathPrefix>
        <mainClass>com.keqing.twinweb.TwinWebApplication</mainClass>
      </manifest>
      <manifestEntries>
        <Class-Path>./config/</Class-Path>
      </manifestEntries>
    </archive>
    <excludes>
      <exclude>*.yml</exclude>
      <exclude>mapper/**</exclude>
      <exclude>*.xml</exclude>
    </excludes>
  </configuration>
</plugin>

3. Use maven-dependency-plugin to copy the JARs into libs

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-dependency-plugin</artifactId>
  <executions>
    <execution>
      <id>copy-dependencies</id>
      <phase>package</phase>
      <goals>
        <goal>copy-dependencies</goal>
      </goals>
      <configuration>
        <outputDirectory>${project.build.directory}/twin-web/libs</outputDirectory>
        <excludeTransitive>false</excludeTransitive>
        <stripVersion>false</stripVersion>
        <includeScope>runtime</includeScope>
      </configuration>
    </execution>
  </executions>
</plugin>

Run the resulting JAR with the same command as before:

java -jar xxx.jar

Method 3: Maven Assembly Plugin

The maven-assembly-plugin can create a single archive (ZIP or TAR.GZ) that contains the executable JAR, the config directory, and all dependency JARs under lib . Example configuration snippets:

<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <configuration>
    <appendAssemblyId>false</appendAssemblyId>
    <descriptors>
      <descriptor>assembly.xml</descriptor>
    </descriptors>
  </configuration>
  <executions>
    <execution>
      <id>make-assembly</id>
      <phase>package</phase>
      <goals>
        <goal>single</goal>
      </goals>
    </execution>
  </executions>
</plugin>

The accompanying assembly.xml defines file sets for the compiled JAR, configuration files, scripts, and dependency sets, specifying permissions and output directories.

After building, unzip the archive and start the application with:

java -jar -Dloader.path=./lib -jar xxx.jar

All three approaches achieve the same goal: separating heavy dependency JARs and configuration files from the core executable, reducing upload size, and speeding up deployment cycles.

backend developmentconfigurationdependency-managementMavenSpring Bootpackaging
Code Ape Tech Column
Written by

Code Ape Tech Column

Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn

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.