Spring Boot Packaging with Maven Assembly Plugin and Shell Deployment Script
This article explains how to use Maven profiles and the maven‑assembly‑plugin to create environment‑specific zip packages for a Spring Boot application and provides a reusable shell script (shenniu_publish.sh) for extracting, starting, stopping, and managing the deployed JAR on Linux.
The guide starts by describing two ways to manage configuration for multiple deployment environments (development, test, uat, production) in a Spring Boot project, recommending the use of Maven profiles to separate environment‑specific resources.
It shows the Maven <profiles> configuration that defines properties such as activeProfile, package-name, and boot-main, which are later injected into the assembly descriptor and 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>Next, the article configures the maven-jar-plugin and maven-assembly-plugin to produce a zip package that contains the compiled JAR, external configuration files, and the deployment scripts while excluding source configuration files from the main JAR:
<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 layout, placing configuration files under conf, libraries under lib, the start‑up script under the root, and the built JAR in a profile‑named directory:
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0" ...>
<id>${activeProfile}</id>
<formats><format>zip</format></formats>
<includeBaseDirectory>false</includeBaseDirectory>
<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>The core of the deployment process is the shenniu_publish.sh script, which automates unzipping, setting execution permissions, detecting running processes, and launching the application using either java -cp or java -jar based on a user‑selected language type (java, javac, netcore). Key functions include: shenniu_unzip – extracts the zip package to a target directory. getPid – finds the PID of the running JAR. start – stops any existing instance, prompts for the language type, and starts the service. stop – kills the running process.
Command‑line handling for start, stop, restart, and unzip actions.
#!/usr/bin/env bash
# 可变参数变量
languageType="javac" # 支持 java,javac,netcore 发布
baseZipName="${package-name}-${activeProfile}"
packageName="${package-name}"
mainclass="${boot-main}"
...
function shenniu_unzip() {
echo "解压---------------------------------------------"
if [ ! `find ${baseZipPath}` ]; then
echo "不存在压缩包:${baseZipPath}"
else
unzip -od ${baseDirPath}/${baseZipName} ${baseZipPath}
chmod +x ${baseDirPath}/${baseZipName}/${packageName}
echo "解压完成。"
fi
}
...
if [ ${#} -ge 1 ]; then
case ${1} in
"start") start ;;
"restart") start ;;
"stop") stop ;;
"unzip") shenniu_unzip ; start ;;
*) echo "${1}无任何操作" ;;
esac
else
echo "command如下命令:
unzip:解压并启动
start:启动
stop:停止进程
restart:重启
示例命令如:./shenniu_publish start"
fiAfter uploading the generated zip to a Linux server, the user can unzip it with:
unzip -od eureka-server-0.0.1-node eureka-server-0.0.1-node.zipIf the script shows Windows line‑ending errors, converting it to Unix format with vim (using set ff=unix) resolves the issue.
Finally, the article encourages readers to try the script, use the provided GitHub repository for testing, and share feedback.
IT Xianyu
We share common IT technologies (Java, Web, SQL, etc.) and practical applications of emerging software development techniques. New articles are posted daily. Follow IT Xianyu to stay ahead in tech. The IT Xianyu series is being regularly updated.
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.
