Spring Boot Packaging with Maven Assembly Plugin and Deployment via Shell Script
This guide demonstrates how to configure Maven profiles for different environments, use the Maven assembly and jar plugins to create a zip package of a Spring Boot application, and deploy and manage the service on Linux using a custom shell script called shenniu_publish.sh.
This article explains how to package a Spring Boot application into a zip file using Maven's maven-assembly-plugin and maven-jar-plugin, configure environment‑specific profiles, and deploy the artifact on a Linux server with a custom shell script named shenniu_publish.sh.
Profiles for environment configuration
Define profiles in pom.xml to separate configuration files for development, testing, UAT, and production.
Activate a profile via -P<profile-id> or by setting spring.profiles.active in application.yml.
Maven assembly plugin configuration
The assembly descriptor ( assembly.xml) specifies the files to include in the zip, such as configuration directories, scripts, and the built jar. Key nodes include: formats: output format (zip). fileMode: set executable permission (777) for the script. filtered: replace Maven properties (e.g., ${package-name}) inside the script.
Example snippet of assembly.xml:
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
<id>${activeProfile}</id>
<formats>zip</formats>
<fileMode>777</fileMode>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.basedir}/src/main/assembly/${activeProfile}</directory>
<outputDirectory>${package-name}-${activeProfile}</outputDirectory>
<includes>
<include>**/*</include>
</includes>
</fileSet>
</fileSets>
</assembly>Shell script shenniu_publish.sh
The script provides commands to unzip the package, start, stop, restart, and check the status of the Spring Boot jar. It reads parameters passed from Maven properties, sets execution permissions, and supports three launch modes: java -cp, java -jar, and netcore.
#!/usr/bin/env bash
# Variable definitions (filled by Maven)
languageType="javac" # java, javac, netcore
baseZipName="${package-name}-${activeProfile}"
packageName="${package-name}"
mainclass="${boot-main}"
# Paths
basePath=$(cd $(dirname $0); pwd)
baseZipPath="${basePath}/${baseZipName}.zip"
baseDirPath="${basePath}"
# Functions
function shenniu_unzip() { ... }
function getPid() { ... }
function start() { ... }
function stop() { ... }
# Main entry
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: ./shenniu_publish.sh {start|stop|restart|unzip}"
fiUsage steps
Build the project with the desired profile: mvn clean package -Puat.
The assembly plugin creates ${package-name}-${profile}.zip containing the jar, configuration files, and shenniu_publish.sh.
Transfer the zip to the Linux server and unzip: unzip -od <target-dir> <zip-file>.
Convert the script to Unix line endings if edited on Windows: vim shenniu_publish.sh → :set ff=unix → :wq.
Run the script: ./shenniu_publish.sh start to launch the service, or use stop, restart, unzip as needed.
The article concludes that once the script runs without errors, the Spring Boot service can be managed efficiently, and the source code is available in a Git repository for further exploration.
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.
IT Architects Alliance
Discussion and exchange on system, internet, large‑scale distributed, high‑availability, and high‑performance architectures, as well as big data, machine learning, AI, and architecture adjustments with internet technologies. Includes real‑world large‑scale architecture case studies. Open to architects who have ideas and enjoy sharing.
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.
