#!/bin/bash # ============================================================================== # maven-install.sh # # Installs or updates Apache Maven to the latest 3.x release. # # Usage: # curl -fsSL https://curl-pipe-bash.watzinger.biz/maven-install.sh | sudo bash # # What it does: # - Detects the latest Maven 3 version from Apache's dist index # - Skips installation if the latest version is already installed # - Verifies the SHA-512 checksum of the downloaded tarball # - Installs to /opt/maven/ and symlinks /opt/maven/latest # - Removes the previous versioned install directory after update # - Adds M2_HOME and PATH to /etc/profile.d/maven.sh (system-wide, no # sourcing-in-subshell problem) # - Cleans up temp files on exit, even on failure # # Requirements: curl, tar, sha512sum (coreutils); sudo only when installation or # environment writes are needed # ============================================================================== set -euo pipefail # ── Configuration ────────────────────────────────────────────────────────────── APACHE_DIST="https://archive.apache.org/dist/maven" APACHE_DIST_INDEX="https://downloads.apache.org/maven/maven-3/" INSTALL_BASE="/opt/maven" SYMLINK_NAME="latest" PROFILE_SCRIPT="/etc/profile.d/maven.sh" TMP_DIR="$(mktemp -d)" TARGET_USER="" TARGET_HOME="" # ── Cleanup on exit ──────────────────────────────────────────────────────────── cleanup() { rm -rf "$TMP_DIR" } trap cleanup EXIT # ── Helper: print to stderr ──────────────────────────────────────────────────── err() { echo "[ERROR] $*" >&2; } info() { echo "[INFO] $*"; } is_root() { [ "$(id -u)" -eq 0 ] } run_privileged() { if is_root; then "$@" else sudo "$@" fi } resolve_target_user() { if is_root && [ -n "${SUDO_USER:-}" ] && [ "$SUDO_USER" != "root" ]; then TARGET_USER="$SUDO_USER" TARGET_HOME="$(getent passwd "$SUDO_USER" 2>/dev/null | cut -d: -f6 || true)" if [ -z "$TARGET_HOME" ] && [ -r /etc/passwd ]; then TARGET_HOME="$(grep "^${SUDO_USER}:" /etc/passwd | cut -d: -f6 || true)" fi if [ -z "$TARGET_HOME" ]; then err "Could not determine home directory for sudo user: $SUDO_USER" exit 1 fi else TARGET_USER="${USER:-$(id -un)}" TARGET_HOME="$HOME" fi } # ── Dependency check ─────────────────────────────────────────────────────────── check_dependencies() { local missing=() for cmd in curl tar sha512sum; do command -v "$cmd" &>/dev/null || missing+=("$cmd") done if [ ${#missing[@]} -gt 0 ]; then err "Missing required tools: ${missing[*]}" err "Install them and re-run this script." exit 1 fi } # ── Detect latest Maven version ──────────────────────────────────────────────── # Scrapes the Apache dist directory index, which is a stable, machine-readable # listing (unlike download.cgi which is a human-facing redirect page). get_latest_maven_version() { info "Fetching latest Maven version from Apache dist index..." LATEST_VERSION=$( curl -fsSL "$APACHE_DIST_INDEX" \ | grep -oP '(?<=href=")[0-9]+\.[0-9]+\.[0-9]+(?=/")' \ | sort -V \ | tail -1 ) if [ -z "$LATEST_VERSION" ]; then err "Could not determine latest Maven version. Check your internet connection." exit 1 fi info "Latest Maven version: $LATEST_VERSION" } # ── Detect installed Maven version ───────────────────────────────────────────── get_installed_maven_version() { local mvn_bin="$INSTALL_BASE/$SYMLINK_NAME/bin/mvn" if [ -x "$mvn_bin" ]; then INSTALLED_VERSION=$("$mvn_bin" --version 2>/dev/null \ | grep -oP 'Apache Maven \K[0-9]+\.[0-9]+\.[0-9]+' || true) info "Installed Maven version: ${INSTALLED_VERSION:-unknown}" else INSTALLED_VERSION="" info "No existing Maven installation found." fi } # ── Download and verify tarball ──────────────────────────────────────────────── download_and_verify() { local ver="$1" local major major=$(echo "$ver" | cut -d. -f1) local base_url="$APACHE_DIST/maven-${major}/${ver}/binaries" local tarball="apache-maven-${ver}-bin.tar.gz" local tarball_path="$TMP_DIR/$tarball" local checksum_path="$TMP_DIR/${tarball}.sha512" info "Downloading Maven $ver..." curl -fsSL --progress-bar \ "${base_url}/${tarball}" -o "$tarball_path" info "Downloading SHA-512 checksum..." curl -fsSL \ "${base_url}/${tarball}.sha512" -o "$checksum_path" info "Verifying checksum..." # sha512sum expects: # Apache ships the hash alone (no filename), so we construct the line. local expected_hash expected_hash=$(cat "$checksum_path") echo "${expected_hash} ${tarball_path}" | sha512sum --check --status info "Checksum verified OK." TARBALL_PATH="$tarball_path" } # ── Install or update Maven ──────────────────────────────────────────────────── install_or_update_maven() { if [ "$LATEST_VERSION" = "$INSTALLED_VERSION" ]; then info "Maven $INSTALLED_VERSION is already up to date. Nothing to do." return 0 fi download_and_verify "$LATEST_VERSION" local new_install_dir="$INSTALL_BASE/$LATEST_VERSION" info "Ensuring $INSTALL_BASE exists..." run_privileged mkdir -p "$INSTALL_BASE" info "Extracting Maven $LATEST_VERSION..." # Extract into the install base, then rename so the target dir name is # just the version number, not the default apache-maven-. run_privileged tar -xzf "$TARBALL_PATH" -C "$INSTALL_BASE" run_privileged mv "$INSTALL_BASE/apache-maven-$LATEST_VERSION" "$new_install_dir" # Capture the old versioned dir before the symlink is updated local old_install_dir="" if [ -L "$INSTALL_BASE/$SYMLINK_NAME" ]; then old_install_dir=$(readlink -f "$INSTALL_BASE/$SYMLINK_NAME" || true) fi info "Updating symlink: $INSTALL_BASE/$SYMLINK_NAME -> $new_install_dir" run_privileged rm -f "$INSTALL_BASE/$SYMLINK_NAME" run_privileged ln -s "$new_install_dir" "$INSTALL_BASE/$SYMLINK_NAME" # Remove the previous versioned directory (skip if it equals the new one, # which can happen if the tarball was already extracted manually) if [ -n "$old_install_dir" ] \ && [ "$old_install_dir" != "$new_install_dir" ] \ && [ -d "$old_install_dir" ]; then info "Removing old install: $old_install_dir" run_privileged rm -rf "$old_install_dir" fi info "Maven $LATEST_VERSION installed successfully." } # ── Configure system-wide environment ───────────────────────────────────────── # /etc/profile.d/ is sourced for all login shells; ~/.bashrc covers # non-login interactive shells (desktop terminals). Both point to the # /opt/maven/latest symlink so they never need updating on version upgrades. configure_environment() { local target="$INSTALL_BASE/$SYMLINK_NAME" local bashrc="$TARGET_HOME/.bashrc" local bashrc_marker="# maven-install.sh" # /etc/profile.d/ - login shells (su -, ssh, TTY login) if [ -f "$PROFILE_SCRIPT" ] && grep -q "M2_HOME" "$PROFILE_SCRIPT"; then info "System profile already configured in $PROFILE_SCRIPT." else info "Writing environment config to $PROFILE_SCRIPT..." run_privileged tee "$PROFILE_SCRIPT" > /dev/null < "$bashrc" if is_root && [ "$TARGET_USER" != "root" ]; then chown "$TARGET_USER:$(id -gn "$TARGET_USER")" "$bashrc" fi fi if grep -q "$bashrc_marker" "$bashrc" 2>/dev/null; then info "$bashrc already sources $PROFILE_SCRIPT." else info "Adding source line to $bashrc for non-login shells..." cat >> "$bashrc" < /usr/bin/mvn..." run_privileged ln -sf "$INSTALL_BASE/$SYMLINK_NAME/bin/mvn" /usr/bin/mvn } # ── Main ─────────────────────────────────────────────────────────────────────── main() { check_dependencies resolve_target_user get_latest_maven_version get_installed_maven_version install_or_update_maven configure_environment link_mvn_binary info "Done. Maven $LATEST_VERSION is ready." info " Versioned install : $INSTALL_BASE/$LATEST_VERSION" info " Symlink : $INSTALL_BASE/$SYMLINK_NAME -> $INSTALL_BASE/$LATEST_VERSION" } main "$@"