172

Is it possible to set the location of the local Maven repository as argument on the Maven command line?

The thing is that I don't use the default one in ~/.m2/repository. However I checked out some project that is being built with its own settings with -s settings.xml. That settings.xml doesn't specify my local repository, so Maven uses uses ~/.m2/repository again... I would like to use a non-default local repository location without having to add a <localRepository> element in the project's settings.xml

I've tried

  • -DlocalRepository="..."
  • $mvn invoker:run -s settings.xml clean install -DskipTests -DlocalRepositoryPath=
  • -Dsettings.localRepository

but none of these options works.

So I think I have to decide whether I will be modifying a third party settings.xml or move my local repo to ~

oberlies
  • 11,503
  • 4
  • 63
  • 110
lisak
  • 21,611
  • 40
  • 152
  • 243
  • Obviously this is not your fault, but Maven projects shouldn't be providing a `settings.xml`. See http://maven.apache.org/guides/mini/guide-configuring-maven.html – oberlies Mar 10 '15 at 10:57

3 Answers3

332

use maven property maven.repo.local:

mvn -Dmaven.repo.local=$HOME/.my/other/repository clean install

No modifications to settings.xml are necessary.

Petr Kozelka
  • 7,670
  • 2
  • 29
  • 44
  • 10
    The local repository must be an absolute path, https://maven.apache.org/guides/mini/guide-configuring-maven.html. – luka5z Dec 28 '16 at 16:21
  • 7
    On Windows use ` mvn -D"maven.repo.local"=%USER_HOME%/.my/other/repository clean install ` – Igor Bljahhin Apr 26 '17 at 15:36
  • Using maven-3.6.3 the local repository dir works as a relative path (on MacOS at least) – Ed Randall Jun 03 '20 at 15:35
  • 1
    Here is the official docs that mention this: https://maven.apache.org/ref/current/maven-model-builder/index.html – Rafał Kłys Jul 14 '21 at 05:58
  • 2
    @powder366 Environment variable? `MAVEN_OPTS=-D"maven.repo.local"=%USER_HOME%/.my/other/repository` might work. Or `~/.mavenrc`? – user7610 Nov 22 '21 at 11:58
14

For git:

alias mvn='mvn "-Dmaven.repo.local=$(git rev-parse --show-toplevel)/.m2/repository"'

This uses a separate maven repository in each git repository

Clashsoft
  • 11,553
  • 5
  • 40
  • 79
Andrew Sherman
  • 141
  • 1
  • 4
2

One kind of hacky way that would work is:

  1. Add <localRepository>${m2.localRepository}</localRepository> to your settings.xml
  2. In your mvn.sh or mvn.bat, add -Dm2.localRepository=<full path to home dir>/.m2/repository before the "$@" in the command that gets executed. This will make your default local repo stay where it should be.
  3. Now you can use mvn -Dm2.localRepository=... <phases/goals>. Because your command line system property gets added to the command line after the one in the mvn script, it will take precedence. (At least I'm pretty sure it works that way on both windows and linux, but testing will tell.)
Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
  • That doesn't help, I added -Dm2.localRepository="custom" before "$@" into mvn script, still uses ~/.m2/repository ... then I also included -Dm2.localRepository="custom" when running $mvn, still uses ~/.m2/repository ...simply without localRepository element in the settings.xml, there is no way of changing it – lisak Jul 26 '11 at 00:31
  • @lisak: Did you do step 1 of adding the localRepository element to the settings.xml? I tested this, and it works, except that you can't actually use the '~' alias in the mvn script. You have to put an absolute path in, though you could probably do some other shell/batch script trickery to get around that. – Ryan Stewart Jul 26 '11 at 01:09
  • It works, but I really don't have explanation... If I declare localRepository in $M2_HOME/conf/settings.xml without step 2, it is overriden by -s settings.xml and doesn't work, though with step 2 it works ... but only step 2 without localRepository in $M2_HOME/conf/settings.xml doesn't work ... both steps 1,2 must be applied for it to work and I can even change the location then via $mvn -Dm2.localRepository=.. thanks Ryan – lisak Jul 26 '11 at 01:26