Skip to content

Instantly share code, notes, and snippets.

@hbenali
Last active January 27, 2026 10:15
Show Gist options
  • Select an option

  • Save hbenali/9276b350c24ce12a7fbe82e26b43cd4c to your computer and use it in GitHub Desktop.

Select an option

Save hbenali/9276b350c24ce12a7fbe82e26b43cd4c to your computer and use it in GitHub Desktop.
mvn-exo-docker-deploy-script
#!/usr/bin/env bash
set -euo pipefail
# Ensure we are in a directory with a pom.xml
if [[ ! -f "pom.xml" ]]; then
echo "Error: No pom.xml found in the current directory ($PWD)."
echo "Please run this script from the root of a Maven module."
exit 1
fi
CONTAINER=exo
TEMP_DIR=$(mktemp -d)
DEPLOYED=0
TARGET_DIR="$PWD"
echo "============================"
echo "Starting Docker deploy..."
echo "Container: $CONTAINER"
echo "Target directory: $TARGET_DIR"
echo "============================"
# Function to deploy files to container
deploy_files() {
local pattern=$1
local dest_dir=$2
shift 2
local exclude_patterns=("$@")
# Build exclude args for find
EXCLUDE_ARGS=()
for pat in "${exclude_patterns[@]}"; do
EXCLUDE_ARGS+=(! -name "$pat")
done
# Only search in directories starting with "target"
files_found=$(find "$TARGET_DIR" -type f -path "*/target*/*$pattern" \
! -path "*/WEB-INF/*" \
"${EXCLUDE_ARGS[@]}" 2>/dev/null)
count=$(echo "$files_found" | grep -cve '^\s*$' || true)
if [[ "$count" -gt 0 ]]; then
echo "Deploying $count files matching $pattern to $dest_dir"
DEPLOYED=1
for f in $files_found; do
echo "Copying $f to $CONTAINER:$dest_dir"
docker cp "$f" "$CONTAINER":"$dest_dir"
docker exec "$CONTAINER" chown 999:999 "$dest_dir/$(basename "$f")"
done
else
echo "No files found for pattern $pattern (excluding WEB-INF and excluded patterns)"
fi
}
# Check for ZIP first
ZIP_FILE=$(find "$TARGET_DIR" -type f -path "*/target*/*.zip" 2>/dev/null | head -n 1)
if [[ -n "$ZIP_FILE" ]]; then
echo "ZIP artifact found: $ZIP_FILE"
echo "Extracting ZIP to temp dir: $TEMP_DIR"
unzip -j "$ZIP_FILE" -d "$TEMP_DIR"
JARS=$(find "$TEMP_DIR" -type f -name "*.jar" 2>/dev/null)
WARS=$(find "$TEMP_DIR" -type f -name "*.war" 2>/dev/null)
if [[ -n "$JARS" ]]; then
echo "Deploying JARs extracted from ZIP to /opt/exo/lib"
for f in $JARS; do
echo "Copying $f to $CONTAINER:/opt/exo/lib"
docker cp "$f" "$CONTAINER":/opt/exo/lib
docker exec "$CONTAINER" chown 999:999 /opt/exo/lib/$(basename "$f")
DEPLOYED=1
done
fi
if [[ -n "$WARS" ]]; then
echo "Deploying WARs extracted from ZIP to /opt/exo/webapps"
for f in $WARS; do
echo "Copying $f to $CONTAINER:/opt/exo/webapps"
docker cp "$f" "$CONTAINER":/opt/exo/webapps
docker exec "$CONTAINER" chown 999:999 /opt/exo/webapps/$(basename "$f")
DEPLOYED=1
done
fi
if [[ "$DEPLOYED" -eq 0 ]]; then
echo "ZIP extracted but no JAR/WAR found inside"
fi
rm -rf "$TEMP_DIR"/*
else
echo "No ZIP artifact found, looking for JARs and WARs in target directories..."
# Deploy JARs (skip sources, tests, and WEB-INF)
deploy_files "*.jar" "/opt/exo/lib" "*-sources.jar" "*-tests.jar"
# Deploy WARs (skip sources and WEB-INF)
deploy_files "*.war" "/opt/exo/webapps" "*-sources.war"
fi
# Restart container only if something was deployed
if [[ "$DEPLOYED" -eq 1 ]]; then
echo "Restarting Docker container '$CONTAINER'"
docker restart "$CONTAINER"
else
echo "No artifacts deployed. Skipping Docker restart."
fi
echo "============================"
echo "Docker deploy completed."
echo "============================"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment