Skip to content

Instantly share code, notes, and snippets.

@h8rt3rmin8r
Last active August 30, 2020 23:07
Show Gist options
  • Save h8rt3rmin8r/f5677a42bcc5ca5cd71ed8a2af5523ab to your computer and use it in GitHub Desktop.
Save h8rt3rmin8r/f5677a42bcc5ca5cd71ed8a2af5523ab to your computer and use it in GitHub Desktop.
Install the latest version of Apache Maven on Ubuntu
#! /usr/bin/env bash
#
# [ install-maven.sh ]
#
# Install Apache Maven on Ubuntu
#
# Created on 20200830 by h8rt3rmin8r (161803398@email.tg)
#
# Reference:
#
# https://maven.apache.org/download.cgi
# https://linuxize.com/post/how-to-install-apache-maven-on-ubuntu-20-04/
#
# Source:
#
# http://bit.ly/github-install-maven
# http://bit.ly/github-install-maven-raw
# http://bit.ly/pastebin-install-maven
# http://bit.ly/pastebin-install-maven-raw
#
#-------------------------------------------------------------------------------
# Declare functions
function imaven_cleanup() {
# Remove downloaded file from /tmp
rm "${lm_filepath}" &>/dev/null
return $?
}
function imaven_download() {
# Download the latest version of Maven
wget --show-progress "${lm_link}" -P /tmp
return $?
}
function imaven_environment() {
# Write environment variables script for Maven
local out_file="/etc/profile.d/maven.sh"
sudo echo '#! /usr/bin/env bash' > "${out_file}"
sudo echo '#' >> "${out_file}"
sudo echo '# [ maven.sh ]' >> "${out_file}"
sudo echo '#' >> "${out_file}"
sudo echo "# Established on $(date '+%Y-%m-%d %H:%M:%S') by $(whoami)" >> "${out_file}"
sudo echo '#' >> "${out_file}"
sudo echo "" >> "${out_file}"
sudo echo 'export JAVA_HOME=/usr/lib/jvm/default-java' >> "${out_file}"
sudo echo 'export M2_HOME=/opt/maven' >> "${out_file}"
sudo echo 'export MAVEN_HOME=/opt/maven' >> "${out_file}"
sudo echo 'export PATH=${M2_HOME}/bin:${PATH}' >> "${out_file}"
sudo echo "" >> "${out_file}"
sudo chmod +x /etc/profile.d/maven.sh
return $?
}
function imaven_extract() {
# Extract the downloaded Maven source code
sudo tar xf "${lm_filepath}" -C /opt
return $?
}
function imaven_latestversion() {
# Look for the latest version of Maven
curl -s 'https://maven.apache.org/download.cgi' \
| grep -E '<h2.*h2>' \
| sed 's/^.*Maven\ //;s/<.*//'
return $?
}
function imaven_softcheck() {
# Check for the existance of an input software on the local system
local i_n="${1}"
if [[ "x${i_n}" == "x" ]]; then
imaven_vb -e "Encountered an internal error: ${FUNCNAME}"
return 1
fi
${i_n} -version &>/dev/null
local e_c="$?"
return ${e_c}
}
function imaven_symlink() {
# Create a symbolic link to the latest version of Maven
sudo ln -s "/opt/apache-maven-${lm_version}" /opt/maven
return $?
}
function imaven_vb() {
# Verbosity handler
if [[ "${1}" =~ ^[-]+[eisw]$ ]]; then
local vb_type_pre="${1//[-]}"
local vb_type="${vb_type_pre^^}"
else
local vb_type="I"
fi
local vb_message="$@"
local vb_timestamp="$(date '+%s.%N')"
local vb_output="${vb_timestamp}|${vb_type}|${sh_name}|${vb_message}"
echo "${vb_output}" &>/dev/stderr
return $?
}
#-------------------------------------------------------------------------------
# Declare variables
sh_name="${0//*\/}"
lm_version=$(imaven_latestversion)
lm_build="${lm_version//[.]*}"
lm_domain="https://www-us.apache.org"
lm_file="apache-maven-${lm_version}-bin.tar.gz"
lm_filepath="/tmp/${lm_file}"
lm_linkpath="/dist/maven/maven-${lm_build}/${lm_version}/binaries/${lm_file}"
lm_link="${lm_domain}${lm_linkpath}"
soft_check_a=$(imaven_softcheck "java")
soft_check_b=$(imaven_softcheck "mvn")
#-------------------------------------------------------------------------------
# Execute operations
imaven_vb -i "Beginning Maven installation process ..."
## Make sure the latest version of Maven was found on the remote host
if [[ "x${lm_version}" == "x" || "x${lm_build}" == "x" ]]; then
imaven_vb -e "Unable to determine the latest version of Maven"
imaven_vb -i "Check your Internet connection and try again"
exit 1
else
imaven_vb -i "Discovered latest Maven version: ${lm_version}"
fi
## Check if Maven is already installed on the local system
if [[ "${soft_check_b}" -eq 0 ]]; then
imaven_vb -e "Maven is already installed on the local system"
imaven_vb -e "Terminating installation processes"
exit 1
fi
## Update the local package list
imaven_vb -i "Updating the local package list (requires sudo)"
sudo apt update
## If JDK isn't installed, kick off the installation
if [[ "${soft_check_a}" -ne 0 ]]; then
imaven_vb -w "Required software 'default-jdk' not found"
imaven_vb -i "Installing 'default-jdk' (requires sudo)"
sudo apt install default-jdk
fi
## Download the latest Maven source code
imaven_download
## Extract the download into the '/opt' directory and delete the tmp file
imaven_extract
imaven_cleanup
## Create a symbolic link for the latest version of Maven
imaven_symlink
## Build environment variables script for Maven
imaven_environment
## Source the environment variables script
source /etc/profile.d/maven.sh
## Verify the installation process was completed successfully
soft_check_b=$(imaven_softcheck "mvn")
if [[ "${soft_check_b}" -ne 0 ]]; then
imaven_vb -e "An unknown error has occurred; installation may be corrupt"
imaven_vb -e "Terminating installation processes"
exit 1
else
imaven_vb -s "Installation of Maven version ${lm_version} is COMPLETE"
imaven_vb -i "IMPORTANT NOTE: Add the following line to ${HOME}/.bashrc:"
echo 'source /etc/profile.d/maven.sh' &>/dev/stderr
exit 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment