How to Install Apache Maven on macOS

Apache Maven is a build automation tool used primarily for Java projects. In this tutorial, we will learn how to install Apache Maven on macOS.

Prerequisites

Before we begin, ensure you have the following prerequisites:

Step 1: Download Apache Maven

Visit the official Apache Maven website at http://maven.apache.org/download.cgi and download the latest version of Apache Maven.

Alternatively, you can use the following command to download the latest version of Maven:

$ curl https://apache.org/dyn/mirrors/mirrors.cgi?action=download&filename=maven/maven-3/{module}/binaries/apache-maven-{module}-bin.{format} --output apache-maven-{module}-bin.{format}

Replace {module} with the version of Maven you want to install, and {format} with either zip or tar.gz, depending on which format you prefer.

Step 2: Install Apache Maven

Once you have downloaded the Maven file, extract it to a desired directory using the following command:

$ tar zxvf apache-maven-{module}-bin.{format}

Make sure to replace the {module} and {format} variables with the correct version and file format, respectively.

Step 3: Configure Apache Maven

To configure Apache Maven, we need to set the M2_HOME and PATH environment variables.

Setting M2_HOME

Open up Terminal and create a new file in /etc/profile.d/ using the following command:

$ sudo nano /etc/profile.d/maven.sh

Paste the following line into the file:

export M2_HOME=/path/to/apache-maven-{module}

Make sure to replace /path/to with your actual path to the Maven directory, and replace {module} with the version of Maven you installed.

Save and close the file, then run the following command to source the file:

$ source /etc/profile.d/maven.sh

Setting PATH

Next, we need to add Maven to our PATH environment variable using the following command:

$ export PATH=$PATH:$M2_HOME/bin

This will add the Maven executable to your PATH so you can use it from anywhere in the terminal.

Step 4: Verify Installation

Verify that the installation was successful by running the following command:

$ mvn -version

This should output the version of Maven installed and other relevant information.

Congratulations! You have successfully installed Apache Maven on macOS.

If you want to self-host in an easy, hands free way, need an external IP address, or simply want your data in your own hands, give IPv6.rs a try!

Alternatively, for the best virtual desktop, try Shells!