111

What's the correct Maven environment variable name: MAVEN_HOME, MVN_HOME or M2_HOME?

I've found some details about MAVEN_HOME and M2_HOME here. But I also have seen MVN_HOME around.

cassiomolin
  • 124,154
  • 35
  • 280
  • 359
  • It's depends. Please extract your zip or tar file and then find the env variable inside `bin/mvn` or `bin/mvn.cmd`. Maven home variable name depends on which `M2_HOME|MVN_HOME|MAVEN_HOME` used by it's distribution. During the time of comments I found the most recent version of maven **3.8.4** which one usages `MAVEN_HOME` instead of `M2_HOME` ! – Śhāhēēd Mar 04 '22 at 04:48

8 Answers8

79

I've personally never found it useful to set M2_HOME.

What counts is your $PATH environment. Hijacking part of the answer from Danix, all you need is:

export PATH=/Users/xxx/sdk/apache-maven-3.0.5/bin:$PATH

The mvn script computes M2_HOME for you anyway for what it's worth.

Steve C
  • 18,876
  • 5
  • 34
  • 37
  • 3
    +1. I'd even say setting `M2_HOME` in the past has always lead to weird errors, mostly `NoClassDefFoundError` because the `mvn` command would be found in the PATH, but with another Maven version found through `M2_HOME`. My life has been better since I've stopped setting that env var and only rely on adding `.../bin/mvn`'s directory to `PATH` and let Maven autodetect things. – Baptiste Mathus May 17 '16 at 05:11
  • 8
    "I've personally never found it useful to set M2_HOME." And you was right. That is deprecated : https://stackoverflow.com/a/47712646/270371 – davidxxx May 21 '19 at 08:16
62

M2_HOME (and the like) is not to be used as of Maven 3.5.0. See MNG-5607 and Release Notes for details.

ᄂ ᄀ
  • 5,669
  • 6
  • 43
  • 57
28

Here is my Maven setup. You can use it as an example. You don't need anything else in order to use Maven.

M2_HOME is used for both Maven 2 and 3

export M2_HOME=/Users/xxx/sdk/apache-maven-3.0.5
export M2=$M2_HOME/bin
export MAVEN_OPTS="-Xmx1048m -Xms256m -XX:MaxPermSize=312M"
export PATH=$M2:$PATH
Danix
  • 1,947
  • 1
  • 13
  • 18
10

MAVEN_HOME is used for maven 1 and M2_HOME is used to locate maven 2. Having the two different _HOME variables means it is possible to run both on the same machine. And if you check old mvn.cmd scripts they have something like,

@REM ----------------------------------------------------------------------------
@REM Maven2 Start Up Batch script
@REM
@REM Required ENV vars:
@REM JAVA_HOME - location of a JDK home dir
@REM
@REM Optional ENV vars
@REM M2_HOME - location of maven2's installed home dir
@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands
@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a key stroke before ending
@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven
@REM     e.g. to debug Maven itself, use
@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
@REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files
@REM ----------------------------------------------------------------------------

See that @REM M2_HOME - location of maven2's installed home dir

Anyway usage of this pattern is now deprecated with maven 3.5 as per the documentation.

Based on problems in using M2_HOME related to different Maven versions installed and to simplify things, the usage of M2_HOME has been removed and is not supported any more MNG-5823, MNG-5836, MNG-5607

So now the mvn.cmd look like,

@REM -----------------------------------------------------------------------------
@REM Apache Maven Startup Script
@REM
@REM Environment Variable Prerequisites
@REM
@REM   JAVA_HOME          Must point at your Java Development Kit installation.
@REM   MAVEN_BATCH_ECHO  (Optional) Set to 'on' to enable the echoing of the batch commands.
@REM   MAVEN_BATCH_PAUSE (Optional) set to 'on' to wait for a key stroke before ending.
@REM   MAVEN_OPTS        (Optional) Java runtime options used when Maven is executed.
@REM   MAVEN_SKIP_RC     (Optional) Flag to disable loading of mavenrc files.
@REM -----------------------------------------------------------------------------

So what you need is JAVA_HOME to be set correctly. As per the new installation guide (as of 12/29/2017), Just add the maven bin directory path to the PATH variable. It should do the trick.

ex : export PATH=/opt/apache-maven-3.5.2/bin:$PATH

Spevacus
  • 584
  • 2
  • 13
  • 23
prime
  • 14,464
  • 14
  • 99
  • 131
4

On Windows, the current way to locate settings.xml is to go to %USERPROFILE%. There, a directory .m2 is contained, where one finds settings.xml.

Never use M2_HOME. It is unsupported since Apache Maven 3.5.0:

Based on problems in using M2_HOME related to different Maven versions installed and to simplify things, the usage of M2_HOME has been removed and is not supported any more MNG-5823, MNG-5836, MNG-5607.

Important change for windows users: The usage of %HOME% has been replaced with %USERPROFILE% MNG-6001

Source: https://maven.apache.org/docs/3.5.0/release-notes.html#overview-about-the-changes

Note: I stumbled over the question as chocolatey's Apache Maven package sets M2_HOME - and I wanted to locate settings.xml.

koppor
  • 19,079
  • 15
  • 119
  • 161
-1

$M2_HOMEis used sometimes, for example, to install Takari Extensions for Apache Maven

One way to find $M2_HOME value is to search for mvn:

sudo find / -name "mvn" 2>/dev/null

And, probably it will be: /opt/maven/

imriss
  • 1,815
  • 4
  • 31
  • 46
  • I do not see anything on the linked page suggesting that `M2_HOME` is read by something in Takari. It is just using a placeholder variable in examples of shell commands you could run. – Jesse Glick Apr 15 '19 at 16:56
-2

I have solved same issue with following:

export M2_HOME=/usr/share/maven
  • 2
    While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – double-beep Apr 01 '19 at 08:22
-8

We have M2_HOME,MAVEN_HOME,M3_HOME all are available in market
Previously M2_HOME is the only environment variable used by all as a standard.
But,due latest releases the MAVEN_Home came as standard but some old tools are still trying to find only M2_HOME
so we should have both M2_HOME,MAVEN_HOME to sustain with old and new tools.
M2_HOME can be used for both as well

HariKishore K
  • 399
  • 3
  • 6