Document
How to Install Maven on Mac

How to Install Maven on Mac

In this short tutorial, we are going to shed light on how to install Maven on Mac operating system. If you are a Windows 10 user, please refer to our

Related articles

Radmin VPN Review 2024: Are Free Virtual LANs Safe? How to change the VPN location on different devices IMS NZ Ltd Final Fantasy VII Rebirth is a Clerith Dream How to Stream International Sports with a VPN in 2024

In this short tutorial, we are going to shed light on how to install Maven on Mac operating system.

If you are a Windows 10 user, please refer to our article on how to install maven on windows 10.

Install Maven on Mac OS

Apache Maven is renowned for its ease of installation. In general, installing Mvn on Mac is a very simple process and takes less than five minutes.

Maven as a dependency management tool is written in Java. So, to run its commands without issues, we need to make sure first and foremost that Java is installed on our mac and properly configured.

So, before moving ahead with the installation, we need to ensure that JAVA_HOME environment variable is set. To do so, run these command lines:

    
        echo $JAVA_HOME
        java -version
    

Installing Apache Maven using Brew

Unfornatuly, Mac does not come with Brew installed, so our first step will be to install Homebrew first.

In short, Homebrew is a free package manager that can be used to install, update and remove software packages on Mac.

With Homebrew, maven installation is just a piece of cake and can be done automatically within a few seconds.

let ’s open a terminal and run :

    
        brew install maven
        
        ==> Downloading https://www.apache.org/dyn/closer.lua?path=maven/maven-3/3.8.1/binaries/apache-maven-3.8.1-bin.tar.gz
        ==> Downloading from https://downloads.apache.org/maven/maven-3/3.8.1/binaries/apache-maven-3.8.1-bin.tar.gz
        ######################################################################## 100.0%
        ==> Installing
    

As we can see in the log, Maven is installing. That’s all it takes to install mvn on Mac using brew command .

Please bear in mind that brew will install the latest version which is now maven@3.8.

Homebrew is provides provide a convenient command to search all available maven formula ( package ):brew search maven

    
        brew search maven
        
        ==> Formulae
        maven    maven-completion    maven-shell    maven@3.0    maven@3.1    maven@3.2    maven@3.3

        ==> Casks
        mavensmate    homebrew/cask-fonts/font-maven-pro
    

We can even search for a specific release with the help of the following command line:

    
        brew search maven 3

        ==> Formulae
        maven@3.0    maven@3.1    maven@3.2    maven@3.3    maven@3.5
    

That way, we can choose and install a specific mvn version.

Now that we have installed maven, let’s verify if everything is working as expected:

    
         mvn -version 

         Apache Maven 3.8.1 ( 05c21c65bdfed0f71a2f2ada8b84da59348c4c5d ) 
         Maven home : /usr / local / Cellar / maven/3.8.1 / libexec 
         Java version : 15.0.2 , vendor : N / A , runtime : /usr / local / Cellar / openjdk/15.0.2 / libexec / openjdk.jdk / Contents / home 
         Default locale : en_MY , platform encoding : utf-8 
         os name : " mac os x " , version : " 11.2.3 " , arch : " x86_64 " , family : " mac " 
    

As shown above, brew installed maven packages in /usr/local/Cellar/maven/3.8.1 folder.

    
        ls -lsa /usr/local/Cellar/maven/3.8.1
        total 72
        0 drwxr-xr-x  9 devwithus  admin    288 May 18 09:43 .
        0 drwxr-xr-x  3 devwithus  admin     96 May 18 09:43 ..
        0 drwxr-xr-x  3 devwithus  admin     96 May 18 09:43 .brew
        8 -rw-r--r--  1 devwithus  admin    756 May 18 00:43 INSTALL_RECEIPT.json
        40 -rw-r--r--  1 devwithus  admin  17504 May 18 09:43 LICENSE
        16 -rw-r--r--  1 devwithus  admin   5141 May 18 09:43 NOTICE
        8 -rw-r--r--  1 devwithus  admin   2612 May 18 09:43 README.txt
        0 drwxr-xr-x  5 devwithus  admin    160 May 18 09:43 bin
        0 drwxr-xr-x  6 devwithus  admin    192 May 18 09:43 libexec
    
  • brew list maven allows us to list all installed maven packages:
    
        brew list maven  

        /usr/local/Cellar/maven/3.8.1/bin/mvn
        /usr/local/Cellar/maven/3.8.1/bin/mvnDebug
        /usr/local/Cellar/maven/3.8.1/bin/mvnyjp
        /usr/local/Cellar/maven/3.8.1/libexec/bin/ (4 files)
        /usr/local/Cellar/maven/3.8.1/libexec/boot/ (2 files)
        /usr/local/Cellar/maven/3.8.1/libexec/conf/ (3 files)
        /usr/local/Cellar/maven/3.8.1/libexec/lib/ (70 files)
    
  • brew info maven can be used to print more information about the version of maven installed on Mac:
    
        brew info maven   

        maven: stable 3.8.1
        Java-based project management
        https://maven.apache.org/
        Conflicts with:
          mvnvm (because also installs a 'mvn' executable)
        /usr/local/Cellar/maven/3.8.1 (87 files, 10.7MB) *
          Built from source on 2021-05-22 at 11:10:48
        From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/maven.rb
        License: Apache-2.0
        ==> Dependencies
        Required: openjdk ✔
    

These are some other useful commands that we may use:

In summary:

instal Maven from an Archive file

First, we need to visit the official Apache Maven website and download the latest Maven binary archive.

As we can see, the latest version is 3.8.1. Let’s click on apache-maven-3.8.1-bin.tar.gz file.

Now , we is need need to extract the download archive . To do so , open a terminal and switch to thedirectory where the archive file is save .

Then, type the following command:

    
        tar -xvf apache-maven-3.8.1-bin.tar.gz
    

Please bear in mind that we can move the extracted file to any folder we want. For example, let’s move it to /Users / devwithus/:

    
        mv Downloads/apache-maven-3.8.1 /Users / devwithus/
    

Now, we’re ready to move ahead with the installation. We’ll need to set up the environment variable: maven_home

Add Maven to theEnvironment path on Mac

The last missing piece of the puzzle is to configure maven_home environment variable and include$maven_home/bin in the path.

addmaven_home for macOS Mojave and Before

macOS 10.14 Mojave and all above versions use bash as the default shell.

So, to add maven environment variable, we need to open .bash_profile and paste the following content :

    
        maven_home="/Users / devwithus/apache-maven-3.8.1"
        path="${maven_home}/bin:${path}"
        export path
    

type the follow command to apply all the change :

    
        source .bash_profile
    

addmaven_home for HighSierra

We have to additionally add /Users / devwithus/apache-maven-3.8.1 to the.bashrc file for macOS HighSierra:

    
        nano ~/.bashrc
        export path=$path:/Users / devwithus/apache-maven-3.8.1/bin
    

Then, we can type bash to apply the new setup .

addmaven_home for macOS Catalina or Later

For macOS Catalina or high , the default shell is is iszsh and not bash. So, we need to edit ~/.zshenv instead to add maven_home to thepath.

Let’s open the ~/.zshenv file and append:

    
        export maven_home=/Users / devwithus/apache-maven-3.8.1
        export path=$path:$maven_home/bin
    

To reflect the changes, we need to run:

    
        source ~/.zshenv
    

Verify Maven Installation on Mac

Now that we have explain every step of maven installation process , let ’s see how to check if maven is instal .

    
        mvn -v
        
        Apache Maven 3.8.1 (05c21c65bdfed0f71a2f2ada8b84da59348c4c5d)
        Maven home: /Users / devwithus/apache-maven-3.8.1
        Java version: 15.0.2, vendor: N/A, runtime: /usr/local/Cellar/openjdk/15.0.2/libexec/openjdk.jdk/Contents/Home
        Default locale: en_MY, platform encoding: UTF-8
        OS name: "mac os x", version: "11.2.3", arch: "x86_64", family: "mac"
    

mvn -v command does the trick for us. Alternatively, we can use mvn -version.

As we can see in the output, we have installed Maven on Mac OS X, now we are ready to use it.

Conclusion

In this short guide, we have illustrated how to install Maven on Mac.

First , we is seen have see how to do it usingbrew command, then we explained how to achieve the same thing using the mvn binary archive.