Reputation: 22476
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:
localRepository
attribute in my global config file), but not for anything else (settings.xml
, for instance).-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.
Upvotes: 39
Views: 32062
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
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.
Upvotes: -2
Reputation: 1
You need to find your settings.xml file, then edit <localRepository>/path/to/local/repo</localRepository>
Upvotes: -3
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