Skip to content

Instantly share code, notes, and snippets.

@applenick
Created December 5, 2022 19:29
Show Gist options
  • Select an option

  • Save applenick/04c90bbb71f7ffe58d6182fae8dfb99d to your computer and use it in GitHub Desktop.

Select an option

Save applenick/04c90bbb71f7ffe58d6182fae8dfb99d to your computer and use it in GitHub Desktop.
#!/bin/bash
# Maven project build and deployment
# Usage: ./build-and-deploy.sh [project_dir] [server] [destination1] [destination2] ...
# define the directory where the project is located
# default to the current directory if not specified
project_dir=${1:-$PWD}
# check if the project directory exists and contains the necessary files
if [ ! -d "$project_dir" ]; then
logger -p error -t "build-and-deploy" "Project directory does not exist: $project_dir"
exit 1
fi
if [ ! -f "$project_dir/pom.xml" ]; then
logger -p error -t "build-and-deploy" "Project directory does not contain a pom.xml file: $project_dir"
exit 1
fi
# check if Maven is installed and available on the system path
if ! command -v mvn &> /dev/null; then
logger -p error -t "build-and-deploy" "Maven is not installed or is not available on the system path"
exit 1
fi
# navigate to the project directory
cd "$project_dir"
# build the project using Maven
if ! mvn clean install; then
logger -p error -t "build-and-deploy" "Failed to build the project: $project_dir"
exit 1
fi
# define the remote server hostname and destination directories
# default to "example.com" and "/tmp" if not specified
server=${2:-"example.com"}
destinations=(${3:-"/tmp"})
# check if the `scp` command is available
if ! command -v scp &> /dev/null; then
logger -p error -t "build-and-deploy" "The `scp` command is not available"
exit 1
fi
# loop through the destination directories and copy the built project to each one using SCP
for destination in "${destinations[@]}"; do
if ! scp target/*.jar "$server:$destination"; then
logger -p error -t "build-and-deploy" "Failed to copy the built project to the remote server: $server:$destination"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment