Streamline Spring Boot Deployment with Maven Profiles and a Custom Shell Tool
This guide shows how to package a Spring Boot application using Maven profiles and the maven-assembly-plugin, create a zip release that includes configuration files, the compiled jar and a custom deployment script, and then use the script to unzip, start, stop, and restart the service on Linux.
This article explains how to package a Spring Boot application with Maven profiles and the maven-assembly-plugin, generate a zip release containing configuration files, the compiled jar, and a custom shenniu_publish.sh script, and then use the script to manage the service on Linux.
Profiles for Different Environments
Two ways to separate environment configurations are described; the article focuses on the Maven‑profile method, defining id, properties (e.g., package-name, boot-main), and activeByDefault to select the default profile.
Maven Assembly Plugin Configuration
The maven-jar-plugin is configured to produce a jar without descriptor files, and the maven-assembly-plugin creates a zip package. Important nodes include:
formats : defines the archive format (zip).
dependencySets : packages project dependencies into a lib directory.
fileSets : includes configuration files, the shell script (with executable permissions), and the compiled jar into the zip.
assembly.xml Example
<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 provides functions to unzip the package, detect the running process, start, stop, and restart the application, supporting three launch modes: java -cp, java -jar, and netcore. It reads parameters injected from Maven profiles, such as ${package-name}, ${activeProfile}, and ${boot-main}.
#!/usr/bin/env bash
# Variable parameters passed from Maven
languageType="javac" # java, javac, netcore
baseZipName="${package-name}-${activeProfile}"
packageName="${package-name}"
mainclass="${boot-main}"
... (full script omitted for brevity) ...
fiUsage
After uploading the zip to a Linux server, unzip it with unzip -od <target-dir> <zip-file>. Convert the script to Unix line endings using vim shenniu_publish.sh → set ff=unix → :wq. Then run commands like ./shenniu_publish.sh start, stop, restart, or unzip to manage the service.
For the full source and test cases, see the GitHub repository https://github.com/shenniubuxing3/springcloud-Finchley.SR2 .
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.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
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.
