Getting Started with Maven | Maven Preparation | Installing Maven

Now prepare Maven. Maven is available from the following site.

https://maven.apache.org

Download Maven

Click the “Download” link in the left menu to move to the download page. On the download page, Maven download links are listed under “Files.”

Maven is distributed as compressed archives. Download either the “Binary tar.gz archive” or the “Binary zip archive.” The contents are the same.

After downloading the installation file, extract the archive and place the generated directory in a suitable location. That is all that is required to install Maven.

However, if you stop here, you must type the exact path every time you run the Maven command. Add one more setting so Maven can be run from any location by command name.

The Maven executable files are in the bin directory under the installed directory. Add this directory path to the PATH environment variable.

Setting Environment Variables on Windows

On Windows, environment variables are managed from the System control panel. Open the System control panel.

If you cannot find it in Windows 10, right-click the Start menu, select “Run,” enter control, and run it. The Control Panel appears. Open “System” from there.

Open the System window and click “Advanced.” Then open the “Advanced” tab in the System control panel and click the “Environment Variables” button. In the dialog, find Path in the list, click “Edit,” and append the path to Maven’s bin directory separated by a semicolon. For example, if Maven is stored in C:\maven, write it like this.

... existing path value ...;c:\maven\bin\

After entering the value, click OK, close the dialogs, and close the Control Panel.

Setting Environment Variables on macOS

On macOS, open the hidden bash profile file from the terminal and add the path.

$ vi ~/.bash_profile

Add the following lines at the end of .bash_profile.

PATH=$PATH:/[Maven bin directory]/
export PATH

After writing the file, press Shift + : and save and quit with :wq.

Installing Maven with Homebrew on macOS

Maven can also be installed with Homebrew.

The Homebrew command is as follows.

% brew install maven

When the command runs, Maven is installed while output similar to the following is displayed.

Updating Homebrew...
==> Homebrew is run entirely by unpaid volunteers. Please consider donating:
  https://github.com/Homebrew/brew#donations
==> Auto-updated Homebrew!
Updated 2 taps (homebrew/core and adoptopenjdk/openjdk).
==> New Formulae
fabric-installer      gnupg@2.2             lefthook              lychee                revive                stp                   webhook
==> Updated Formulae
Updated 250 formulae.
==> Updated Casks
Updated 23 casks.

==> Downloading https://ghcr.io/v2/homebrew/core/openjdk/manifests/15.0.2
######################################################################## 100.0%

... omitted ...

For compilers to find openjdk you may need to set:
  export CPPFLAGS="-I/usr/local/opt/openjdk/include"

Checking the Installation with the mvn Command

After configuring environment variables on Windows or macOS, confirm that the Maven command is recognized. Open a command prompt or terminal and run the following command.

$ mvn -version

mvn is the Maven command. If Maven is configured correctly, Maven version information is displayed. The output is similar to the following.

Apache Maven version name
Maven home : ... path to Maven's bin directory ...
Java version : ... JDK version ...
Java home : ... Java path ...
Default locale : ... default locale ...
OS name : ... operating system name ...

An actual result may look like this.

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: ko_KR, platform encoding: UTF-8
OS name: "mac os x", version: "11.2.3", arch: "x86_64", family: "mac"
kimkc@kimkcui-MacBookPro macaront % 

If output like this appears, Maven’s path is configured correctly. If an error message appears, the path may not be set correctly or the Java version may be too old. Check whether the wrong path was added to PATH, and also confirm that Java is up to date.

Check the installation location

% which mvn
/usr/local/bin/mvn