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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With