Installing Apache Maven on MacOS

I am starting to learn Spring.IO and one of the possible build tools is to use Apache Maven. The other is Gradle, however since the developer team I am training under uses Maven for its other projects I thought it would be in my best interest to get familiar with Maven.

I like working with Mac, as I have always enjoyed using my Ubuntu machines in the past. Since I started working with a friend’s company, they are mainly a Mac shop and he is a big proponent of the Apple products I thought I would go ahead and buy a Macbook Pro. I have been very pleased with this machine and have been using over 4 years now and it is just as quick as when I bought it.

So, the download of Maven is actually very easy. Go to the Apache Maven download page and choose the .zip or the .tar.gz file. Once downloaded, it is wise to check the sha512 checksums against the file to be sure you have a copy of the download that was not tampered with. Click the link to display the checksum for the file you downloaded, then open a terminal window to the folder where you downloaded your zip or tar file and enter the following command

shasum -a 512 apache-maven-3.6.0-bin.zip

shasum is the command and the option of “-a 512” tells shasum which schema to use and then follow up with the name of the file you downloaded. As seen above I downloaded the .zip file of version 3.6.0

Match what is produced via the terminal (a very long string of letters and numbers) to what the Apache Maven download website says the checksum should be. If it is a match, then go ahead and proceed. If not a match (and yes, I checked each character in the string. Might be an easy way, but I did manually), then I suggest checking if downloading the file from a valid source or checking with the Apache team if you downloaded from their website.

I then moved the unzipped folder into my /usr/local directory. Next you will then need to add that directory to your $PATH environment variable. The command is easy, but of course I messed it up the first time around and added an extra / in the middle of the path string which would cause the system not to find the maven directory.

Enter the following if you only want to temporarily add the Maven path to your environment.

export PATH=$PATH:/usr/local/apache-maven-3.6.0/bin

However, after you close your terminal window this path will disappear. If you want to permanently add this path to your personal profile and you are using a BASH terminal (default terminal in Mac) then you will need to add the PATH variable to your ~/.bash_profile 

Back in terminal, enter the following

nano ~/.bash_profile

or instead of nano you can use a different text editor. Add the following to you profile

#Adding maven bin to PATH
export PATH=$PATH:/usr/local/apache-maven-3.6.0/bin

Then use CONTROL-x to close the file, then Y for yes to save, then ENTER key to accept the file name and should be done. To test, close the current terminal window and then open a new one (to make sure the settings save between terminal sessions). Enter the following

echo $PATH

The output should include the normal default paths and your new additional maven directory.

echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/apache-maven-3.6.0/bin

Then test that maven is working by entering the following

mvn -v

The output should be similar to the following.

Apache Maven 3.6.0 (97c98ec64a1fdfee7767ce5ffb20918da4f719f3; 2018-10-24T14:41:47-04:00)
Maven home: /usr/local/apache-maven-3.6.0
Java version: 1.8.0_181, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.13.6", arch: "x86_64", family: "mac"

Troubleshooting

Q1. What if you mess up the path

A1. Well, if you added the path in terminal command and not in a profile text file, then likely all you need to do is close the terminal and open a new one. However, if the incorrect path persists, you can create a new path like the default MacOS path (at least the default for my Yosemite MacOS)

export PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

I found this link helpful http://osxdaily.com/2014/08/14/add-new-path-to-path-command-line/

Add a Comment

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