Skip to content

Instantly share code, notes, and snippets.

@kmix
Last active November 20, 2025 02:44
Show Gist options
  • Select an option

  • Save kmix/eecd35a336d11c0bd76d573c68248d36 to your computer and use it in GitHub Desktop.

Select an option

Save kmix/eecd35a336d11c0bd76d573c68248d36 to your computer and use it in GitHub Desktop.
Maven Install Script for Debian
#!/bin/bash
if [ $# -eq 0 ]; then
echo "No version was provided!"
echo -e "\nUsage: mvn_install <version> (e.g. mvn_install 3.8.1)\n"
exit 1
fi
if [ -x "$(command -v java)" ]; then
echo "Java executable found in path"
else
echo "No java executable found in path. Exiting."
exit 1
fi
if [ "$EUID" -eq 0 ]; then
echo "Running as root or under sudo. No sudo refresh required"
else
echo "Not running as root or under sudo. Revalidating sudo credentials:"
sudo -k
sudo -v
fi
SUDO_OUTPUT=$(sudo -n whoami)
echo "$SUDO_OUTPUT"
if [[ $SUDO_OUTPUT != "root" ]]; then
echo "Unable to run command under sudo. Please re-run script or verify that your sudo credentials are being cached"
exit 1
fi
if [ -x "$(command -v mvn)" ]; then
echo "Maven already installed. Details for existing install:"
mvn -v
echo -e "\nThis version will be updated or overwritten.\n"
else
echo "No maven install found. Proceeding with install."
fi
MAVEN_VERSION=$1
MAVEN_PACKAGE="apache-maven-$MAVEN_VERSION-bin.tar.gz"
MAVEN_URL="https://mirror.cogentco.com/pub/apache/maven/maven-3/$MAVEN_VERSION/binaries/$MAVEN_PACKAGE"
echo "Attempting to download $MAVEN_PACKAGE"
/usr/bin/wget -q $MAVEN_URL -P /tmp
if [[ "$?" != 0 ]]; then
echo "File Not Found: $MAVEN_PACKAGE"
exit 1
else
echo "Download successful."
fi
echo "Extracting $MAVEN_PACKAGE to /opt"
sudo /bin/tar -xzf /tmp/apache-maven-$MAVEN_VERSION-bin.tar.gz -C /opt
echo "Cleaning up /tmp"
rm /tmp/$MAVEN_PACKAGE
echo "Setting /opt/maven symbolic link and updating permissions"
sudo ln -fsn /opt/apache-maven-$MAVEN_VERSION /opt/maven
sudo chmod -R 644 /opt/maven
sudo chmod a+x /opt/maven/bin/mvn
sudo chmod -R a+X /opt/maven
echo "Updating profile file"
echo "export PATH=/opt/maven/bin:${PATH}" | sudo tee /etc/profile.d/maven.sh > /dev/null
sudo chmod +x /etc/profile.d/maven.sh
echo "Applying new profile to update the current path"
source /etc/profile.d/maven.sh
echo "Install complete. Current reported maven version is: "
mvn -version
echo -e "\nDone\n"
@kmix
Copy link
Author

kmix commented Jul 13, 2021

To run, simply source the URL and provide a version. For 3.8.1:

source <(curl -s https://gist.githubusercontent.com/kmix/eecd35a336d11c0bd76d573c68248d36/raw/2db9857d2654efaca5282a66358a2ee6e2c91073/mvn_install.sh) 3.8.1

@kmix
Copy link
Author

kmix commented Jul 13, 2021

To remove, simply:

sudo rm -rf /opt/*maven*
sudo rm /etc/profile.d/maven.sh

@kmix
Copy link
Author

kmix commented May 5, 2025

You can find the current version here: https://maven.apache.org/download.cgi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment