Spring Boot Packaging with Maven Profiles and Shell Deployment Script
This article explains how to configure Maven profiles for different environments, use the maven‑assembly‑plugin to create a zip deployment package, and employ a custom shell script (shenniu_publish.sh) to unzip, start, stop, and restart a Spring Boot jar on Linux, streamlining the build‑and‑deploy workflow.
The guide demonstrates how to package a Spring Boot application using Maven profiles to separate configuration for development, testing, UAT, and production environments, and how to generate a zip release package with the maven-assembly-plugin and maven-jar-plugin .
Profiles configuration – Two approaches are described; the article focuses on defining profiles in pom.xml with <profiles> , each containing <id> , <properties> (e.g., activeProfile , package-name , boot-main ), and <activation> settings.
<profiles>
<profile>
<id>node</id>
<properties>
<!-- parameters passed to scripts -->
<activeProfile>node</activeProfile>
<package-name>${scripts_packageName}</package-name>
<boot-main>${scripts_bootMain}</boot-main>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
...
</profiles>Assembly plugin configuration – An assembly.xml is created to bundle the compiled jar, configuration files, and the deployment shell script into a zip file. Important nodes include <formats> (zip), <fileSets> for configuration and scripts, <dependencySets> for external libraries, and permission settings ( <fileMode>777 , <directoryMode>777 ).
<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) – The script defines variables that are replaced by Maven properties, provides functions to unzip the package, detect the running process, start the application (supporting java -cp , java -jar , and netcore modes), stop the process, and restart it. It also includes usage instructions and a simple command‑line interface.
#!/usr/bin/env bash
# 可变参数变量
languageType="javac" # 支持 java,javac,netcore 发布
# 参数值由 pom 文件传递
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 无任何操作" ;;
esac
else
echo "command 如下:\n unzip:解压并启动\n start:启动\n stop:停止进程\n restart:重启\n 示例:./shenniu_publish.sh start"
fiAfter building the project in IntelliJ, selecting the desired profile generates the zip file, which can be transferred to a Linux server, converted to Unix line endings with vim if necessary, and then managed with the provided shell commands to deploy and control the Spring Boot service.
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.