Spring Boot Packaging with Maven Profiles and a Shell Deployment Script
This article explains how to package a Spring Boot application using Maven profiles and the assembly plugin, generate a zip deployment bundle, and deploy it on Linux with a custom shell script that supports start, stop, restart, and unzip operations.
The guide demonstrates a complete workflow for building and deploying a Spring Boot service, starting with Maven profile configuration to select environment‑specific resources and passing parameters such as package-name and boot-main to the build.
In pom.xml a <profiles> section defines several profiles (e.g., node , node1 , node2 ) where each profile sets properties that will be substituted into the assembly and shell scripts:
<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>The maven-assembly-plugin is then used to create a zip file that contains the compiled JAR, configuration files, and the deployment script. The plugin configuration specifies the descriptor file ( assembly.xml ) and the packaging phase:
<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 layout, including a conf directory for configuration files, a lib directory for dependencies, and the shenniu_publish.sh script with executable 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>
<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>Key nodes such as formats (zip), fileMode (executable permission for the script), and filtered (property substitution) are highlighted to show how the script receives values from Maven.
The shenniu_publish.sh script provides functions to unzip the package, detect the running process, start, stop, and restart the application. It supports three launch modes: java -cp , java -jar , and netcore . A simplified excerpt of the script is shown below:
#!/usr/bin/env bash
languageType="javac" # default type
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 has no operation" ;;
esac
else
echo "Usage: ./shenniu_publish.sh {start|stop|restart|unzip}"
fiTo deploy, the generated zip is transferred to a Linux host, converted to Unix line endings with vim ( set ff=unix ), and then executed with commands such as ./shenniu_publish.sh unzip or ./shenniu_publish.sh start . The script logs the process ID and tails the output for verification.
The repository containing the full project and script is available at https://github.com/shenniubuxing3/springcloud-Finchley.SR2 .
Java Architect Essentials
Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow 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.