Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven - alternative .m2 directory

Tags:

maven

How do I globally change the location of maven's .m2 directory?

Maven uses ${user.home}/.m2 for it's settings, repository cache, etc.

I know that I can:

  • point to a different dir for the repository cache (by changing the localRepository attribute in my global config file), but not for anything else (settings.xml, for instance).
  • Use the -s CLI option, but I'd have to do that every time I use maven.

But ideally, I'd like a global solution. Intuitively that should be possible..

I'd like to do this because my company sets my ${user.home} to a shared drive which is prone to network issues.

like image 370
Roy Truelove Avatar asked May 16 '13 15:05

Roy Truelove


People also ask

What is m2 folder in Maven?

m2 folder is the default folder used by maven to store its: settings. xml file which specifies properties, like the central repository to download your dependencies, the location of the so-called localRepository. by default, the localRepository in which maven stores all the dependencies your project might need to run.


2 Answers

Use the MAVEN_OPTS environment variable to set the location of your repository. Set it in your shell startup, and you don't have to put it on the command line every time.

export MAVEN_OPTS="-Dmaven.repo.local=/path/to/repository" 

You said you wanted to change the .m2 for the settings.xml file as well as the repository directory. You want to set the M2_HOME environment variable. The Maven settings documentation says that it controls where Maven looks for settings.xml. From there, you can control the repository location, etc.

like image 198
dbrown0708 Avatar answered Sep 22 '22 23:09

dbrown0708


The best option is to create the .m2 in your user.home as a symlink upfront before using Maven to point to a local folder on your machine.

Otherwise you could create a shell alias for the mvn called mymvn (or m3 or whatever) that passes in the -s as well as the local repo location.

like image 35
Manfred Moser Avatar answered Sep 23 '22 23:09

Manfred Moser