Roy Truelove
Roy Truelove

Reputation: 22476

Maven - alternative .m2 directory

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:

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.

Upvotes: 39

Views: 32062

Answers (4)

dbrown0708
dbrown0708

Reputation: 4764

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.

Upvotes: 77

Arik
Arik

Reputation: 1257

A bit another use-case, but if you are using Jenkins you can set the Use private Maven repository flag and Jenkins will do it for you.

When this option is checked, Jenkins will tell Maven to use $WORKSPACE/.repository as the local Maven repository. This means each job will get its own isolated Maven repository just for itself.

enter image description here

Upvotes: -2

user3729899
user3729899

Reputation: 1

You need to find your settings.xml file, then edit <localRepository>/path/to/local/repo</localRepository>

Upvotes: -3

Manfred Moser
Manfred Moser

Reputation: 29912

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.

Upvotes: 14

Related Questions