Spring Boot Packaging with Maven and Shell Deployment Script Guide
This article explains how to package a Spring Boot application using Maven profiles and the maven‑assembly‑plugin, generate a zip deployment package, and manage the deployment with a custom shell script that supports unzip, start, stop, and restart operations across different environments.
In this tutorial, a top‑level architect shares a complete workflow for packaging a Spring Boot application, configuring Maven profiles for multiple environments (dev, test, uat, prod), and creating a zip release using maven‑assembly‑plugin and maven‑jar‑plugin.
Profiles configuration
<profiles>
<profile>
<id>node</id>
<properties>
<!-- parameters passed to the script -->
<activeProfile>node</activeProfile>
<package-name>${scripts_packageName}</package-name>
<boot-main>${scripts_bootMain}</boot-main>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
... (additional profiles node1, node2) ...
</profiles>The activeProfile determines which configuration folder is used, while package-name and boot-main are injected into the shell script.
Maven assembly plugin configuration
<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>The accompanying assembly.xml defines the zip format, includes the compiled jar, configuration files, and the shenniu_publish.sh script with proper permissions.
<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>Shell script shenniu_publish.sh</strong></p> <p>The script provides functions to unzip the package, detect the running process, start, stop, and restart the Spring Boot jar. It supports three launch modes: <code>java -cp , java -jar , and netcore (for .NET Core).
#!/usr/bin/env bash
# variable parameters
languageType="javac" # java, javac, netcore
baseZipName="${package-name}-${activeProfile}"
packageName="${package-name}"
mainclass="${boot-main}"
...
function shenniu_unzip() { ... }
function getPid() { ... }
function start() { ... }
function stop() { ... }
# command dispatch
if [ $# -ge 1 ]; then
case $1 in
"start") start ;;
"restart") start ;;
"stop") stop ;;
"unzip") shenniu_unzip && start ;;
*) echo "$1 has no operation" ;;
esac
else
echo "Commands: unzip, start, stop, restart"
fiAfter building the zip with Maven, upload it to a Linux server, convert the script to Unix line endings if needed (using vim with set ff=unix), then run ./shenniu_publish.sh unzip followed by ./shenniu_publish.sh start to launch the service. The script also provides log tailing and PID management.
All configuration values (package name, active profile, main class) are supplied by Maven profiles, so the same script works for any environment without manual edits.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
