Streamline Spring Boot Deployment with Maven Profiles and a Custom Shell Script

This article explains how to package a Spring Boot application using Maven profiles, generate a zip bundle with the assembly plugin, and deploy it on Linux with a reusable shell script that can start, stop, restart, or unzip the service, eliminating manual configuration for each environment.

Java Architect Essentials
Java Architect Essentials
Java Architect Essentials
Streamline Spring Boot Deployment with Maven Profiles and a Custom Shell Script

The author shares a practical method for packaging a Spring Boot application and deploying it using a custom shell script.

Profiles for environment configuration

Two ways to specify environment‑specific configuration are described: setting spring.profiles.active in application.yml or defining Maven profiles that select different resource folders. The article adopts the Maven‑profile approach.

Maven configuration

In pom.xml a <profiles> section is added with several <profile> entries. Each profile defines <properties> such as activeProfile, package-name, and boot-main, and activates by default. The maven-jar-plugin is configured to set the main class and exclude configuration files, while the maven-assembly-plugin creates a zip containing the jar, configuration files and 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>
  ... (additional profiles) ...
</profiles>
<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>
    <format>zip</format>
  </formats>
  <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>
      <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 receives the Maven‑generated variables, extracts the zip package, sets execution permissions, detects a running process, and provides four commands: start, stop, restart, and unzip. It supports three launch modes: java -cp, java -jar, and netcore.

#!/usr/bin/env bash
# Variable placeholders filled by Maven
languageType="javac"
baseZipName="${package-name}-${activeProfile}"
packageName="${package-name}"
mainclass="${boot-main}"
... (rest of the script, including functions shenniu_unzip, getPid, start, stop, and command dispatch) ...

After building the zip, upload it to the Linux server, unzip it (e.g.,

unzip -od eureka-server-0.0.1-node eureka-server-0.0.1-node.zip

), convert the script to Unix format if it was edited on Windows ( vim shenniu_publish.shset ff=unix:wq), and run ./shenniu_publish.sh start to launch the service.

Full example repository: https://github.com/shenniubuxing3/springcloud-Finchley.SR2

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

automationmavenspring-bootProfilesAssembly pluginshell-script
Java Architect Essentials
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.