Maven - alternative .m2 directory

How to globally change the location of the maven .m2 ?

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

I know that I can:

  • specify a different directory for the repository cache (by changing the localRepository attribute in my global configuration file), but not for anything else ( settings.xml , for example).
  • Use the -s CLI option, but I will have to do this every time I use maven.

But ideally, I would like a global solution. Intuitively, this should be possible.

I would like to do this because my company installs my $ {user.home} on a shared drive that is prone to network problems.

+15
maven
May 16 '13 at 15:05
source share
3 answers

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

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

+12
May 16 '13 at 15:50
source share

Use the MAVEN_OPTS environment variable to indicate the location of your repository. Install it during shell startup, and you do not need to enter it every time on the command line.

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

Addition. Sorry. You said you want to change .m2 for settings as well as for the repository. You want to set the environment variable M2_HOME . This documentation says the controls are where maven is looking for settings.xml. From there you can control the location of the repository, etc.

+28
May 16 '13 at 15:39
source share

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

-one
Jul 22 '15 at 8:26
source share



All Articles