How Do You Install Maven on an AWS Instance of RHEL?

Problem scenario
You are using RedHat Enterprise Linux 7.x or 8.x in AWS.  How do you install Maven?

Solution
Prerequisite
Make sure Java has been installed. See this link if you need assistance.

Procedures
1.  Create a file named maven.sh with this as the content:

version=3.6.2
curl http://ftp.wayne.edu/apache/maven/maven-3/$version/binaries/apache-maven-$version-bin.tar.gz > /bin/apache-maven-$version-bin.tar.gz

md=$(md5sum /bin/apache-maven-$version-bin.tar.gz | awk '{print $1}')
if [ $md -ne '35c39251d2af99b6624d40d801f6ff02' ]
then
  echo "Installation of Maven has failed.  Check the source of the URL of the apache-maven-$version-bin.tar.gz file."
  exit
fi

cd /bin
tar -xvf apache-maven-$version-bin.tar.gz
mkdir -p /usr/local/apache-maven
mv /bin/apache-maven-$version/* /usr/local/apache-maven

cp /root/.bash_profile /root/bak.bash_profile.bak
echo "Do NOT run this script twice b/c the .bash_profile file will get duplicate entries."
echo "export M2_HOME=/usr/local/apache-maven" >>  ~/.bashrc
echo "export M2=\$M2_HOME/bin" >>  ~/.bashrc
echo "export PATH=\$M2:\$PATH" >>  ~/.bashrc

export M2_HOME=/usr/local/apache-maven
export M2=\$M2_HOME/bin
export PATH=\$M2:\$PATH
ln -s /usr/local/apache-maven/bin/mvn /bin/mvn
# Run above and below command twice.  It may not work in one location.
/usr/bin/ln -s /usr/local/apache-maven/bin/mvn /bin/mvn
echo "Run 'mvn -version' to see if Maven is installed."
echo "Do not be alarmed if you see a message about a return statement"
return

2.  Run this command:  sudo -s source maven.sh

3.  Test it with this command: mvn -version

Leave a comment

Your email address will not be published. Required fields are marked *