Spring Boot Packaging with Maven Assembly Plugin and Shell Deployment Script
This article demonstrates how to package a Spring Boot application using Maven profiles and the assembly plugin to create a zip distribution, and provides a reusable shell script (shenniu_publish.sh) for extracting, starting, stopping, and restarting the service on Linux environments.
The author, a senior architect, shares a practical guide for building and deploying a Spring Boot application by combining Maven profile configurations, the maven-assembly-plugin, and a custom shell script.
1. Maven profile configuration – Define multiple profiles (node, node1, node2) in <profiles>... to specify environment‑specific properties such as activeProfile , package-name , and boot-main . These properties are later injected into the assembly descriptor and the shell script.
<profiles>
<profile>
<id>node</id>
<properties>
<activeProfile>node</activeProfile>
<package-name>${scripts_packageName}</package-name>
<boot-main>${scripts_bootMain}</boot-main>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
...
</profiles>2. Maven plugins – Use maven-jar-plugin to customize the JAR manifest and exclude configuration files, then maven-assembly-plugin to bundle the JAR, dependent libraries, configuration files, and the shell script into a zip archive.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>${scripts_bootMain}</mainClass>
</manifest>
</archive>
<excludes>
<exclude>**/*.yml</exclude>
<exclude>**/*.properties</exclude>
<exclude>**/*.xml</exclude>
<exclude>**/*.sh</exclude>
</excludes>
</configuration>
</plugin>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<descriptors>
<descriptor>${project.basedir}/src/main/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>3. Assembly descriptor (assembly.xml) – Define the zip format, include the compiled JAR, the conf directory with environment‑specific configuration, and the shenniu_publish.sh script. Set fileMode to 777 for executable permissions and enable filtered so Maven replaces placeholders with profile values.
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0" ...>
<id>${activeProfile}</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<useProjectArtifact>false</useProjectArtifact>
<outputDirectory>${package-name}-${activeProfile}/lib</outputDirectory>
<unpack>false</unpack>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<directory>${project.basedir}/src/main/profiles/${activeProfile}</directory>
<outputDirectory>${package-name}-${activeProfile}/conf</outputDirectory>
<includes><include>**/*</include></includes>
</fileSet>
<fileSet>
<directory>${project.basedir}/src/main/scripts</directory>
<outputDirectory></outputDirectory>
<includes><include>**/*</include></includes>
<fileMode>777</fileMode>
<directoryMode>777</directoryMode>
<filtered>true</filtered>
</fileSet>
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>${package-name}-${activeProfile}/</outputDirectory>
<includes><include>*.jar</include></includes>
</fileSet>
</fileSets>
</assembly>4. Shell script (shenniu_publish.sh) – Provides functions to unzip the package, detect the running process, start the application (supporting java -cp and java -jar modes), stop the process, and restart. Parameters such as package-name , activeProfile , and boot-main are injected from Maven properties, so the script does not need manual edits.
#!/usr/bin/env bash
# variable placeholders filled by Maven
languageType="javac"
baseZipName="${package-name}-${activeProfile}"
packageName="${package-name}"
mainclass="${boot-main}"
...
function shenniu_unzip() { ... }
function getPid() { ... }
function start() { ... }
function stop() { ... }
if [ $# -ge 1 ]; then
case $1 in
"start") start ;;
"restart") start ;;
"stop") stop ;;
"unzip") shenniu_unzip ; start ;;
*) echo "$1 no operation" ;;
esac
else
echo "Usage: unzip|start|stop|restart"
fiAfter building with the selected Maven profile, the resulting zip can be transferred to a Linux server, unzipped, and the script executed (e.g., ./shenniu_publish.sh start ) to launch the Spring Boot service. The article also notes converting the script to Unix line endings with vim set ff=unix if edited on Windows.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
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.