Quantcast
Channel: Baeldung
Viewing all articles
Browse latest Browse all 3524

Difference Between M2_HOME, MAVEN_HOME and using the PATH variable

$
0
0

1. Overview

As part of the Apache Maven installation process, we need to configure various environment variables to ensure our Maven installation works smoothly. In this tutorial, we’ll look at three of these variables: M2_HOME, MAVEN_HOME, and PATH. We’ll see how they influence our installation, depending on the version of Maven we’re using.

Let’s start by looking at how we’d configure the earliest version of Maven.

Note: Apache Maven 1.x and Maven 2.x have reached their end of life. Sections 2 and 3 demonstrate configurations only for illustrative purposes and don’t advocate for their use.

2. Maven 1.x

After verifying and extracting a ready-made binary distribution Maven archive, let’s navigate to the Maven directory’s bin folder. From here, we can run a maven command to see if it works out of the box:

$ maven -v

This command will produce an output indicating that we’re missing a required environment variable:

MAVEN_HOME must be set

Here, Maven informs us that the MAVEN_HOME environment variable not only indicates where the Maven directory was extracted, but it’s also a mandatory variable.

After adding this environment variable to our system, let’s run the previous command:

$ maven -v

We’ll get this result:

 __  __
|  \/  |__ _Apache__ ___
| |\/| / _` \ V / -_) ' \  ~ intelligent projects ~
|_|  |_\__,_|\_/\___|_||_|  v. 1.1

This shows that our system will work with the downloaded version of Maven.

Next, let’s consider how the usage of this variable changed in Maven 2.x.

3. Maven 2.x

After we’ve verified and extracted the downloaded Maven binaries, let’s navigate to the bin directory and run the version information command:

$ mvn -v

Unlike in the previous section, Maven 2.x does not complain about the MAVEN_HOME or M2_HOME variable not being set because version 2 made this variable optional. The command gives an output similar to the one below:

Apache Maven 2.2.1 (r801777; 2009-08-06 21:16:01+0200)
Java version: 1.8.0_412
Java home: /home/.sdkman/candidates/java/8.0.412.fx-librca/jre
Default locale: en, platform encoding: UTF-8
OS name: "linux"

In Maven 2.x, the MAVEN_HOME variable was renamed to M2_HOME. This means that to specify the installation location for Maven 2.x, we need to set the M2_HOME environment variable.

Next, let’s have a look at the latest version of Maven.

4. Maven 3.x

After verifying and extracting the binaries, let’s navigate to the bin directory and query the version information again:

$ mvn -v

The command gives an output similar to the one below:

Apache Maven 3.9.8 (36645f6c9b5079805ea5009217e36f2cffd34256)
Maven home: /home/dev-tools/apache-maven-3.9.8
Java version: 1.8.0_412, vendor: BellSoft, 
    runtime: /home/.sdkman/candidates/java/8.0.412.fx-librca/jre
Default locale: en, platform encoding: UTF-8
OS name: "linux"

Similar to version 2, Maven 3.x made the MAVEN_HOME variable optional. In Maven 3.x, MAVEN_HOME replaced M2_HOME as the variable used to specify the installation location.

5. Comparison Summary

Let’s summarize what we’ve discussed in a tabular format:

Maven Version Variable Name Is it Required
1.x MAVEN_HOME Yes
2.x M2_HOME Optional
3.x MAVEN_HOME Optional

6. Setting the PATH Variable

In the previous sections, we executed the Maven command from within the installation’s bin directory. To enable us to run Maven commands from outside the bin directory, we need to add it to our PATH environment variable. The Maven installation tutorial shows how to do this for different operating systems: Windows, Linux, and macOS.

Consequently, we should be able to run Maven commands from anywhere in our system.

7. Conclusion

In this tutorial, we examined three environment variables that affect our Maven installation: M2_HOME, MAVEN_HOME, and PATH. We saw that M2_HOME and MAVEN_HOME refer to the Maven installation directory, depending on the version. Additionally, we explained how setting the PATH variable makes Maven commands available from anywhere in our system.

       

Viewing all articles
Browse latest Browse all 3524

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>