85

Currently we specify the location of local repository in the settings.xml. Is it possible to override this setting via command line or env variable, such that I can use an alternative location sometimes?

Slava Semushin
  • 14,904
  • 7
  • 53
  • 69
Anthony Kong
  • 37,791
  • 46
  • 172
  • 304
  • 2
    Possible duplicate of [Specifying Maven's local repository location as a CLI parameter](http://stackoverflow.com/questions/6823462/specifying-mavens-local-repository-location-as-a-cli-parameter) – Ingo Karkat Jun 08 '16 at 06:53
  • It didn't occur to me why this would be useful until now - if you're working on several branches (a release branch, a trunk branch) and the source tree is complex, trying to get compilation to work can be a nightmare if you're just using `1.0-SNAPSHOT` as your version. Having separate .m2 repos circumvents a lot of this. – Sridhar Sarnobat Jan 17 '19 at 18:25

2 Answers2

148

You would need to specify the maven.repo.local parameter to do this.

mvn package -Dmaven.repo.local=/alternate/repo/location

Here is a related SO question.

Community
  • 1
  • 1
Raghuram
  • 51,854
  • 11
  • 110
  • 122
  • 4
    The local repository must be an absolute path, https://maven.apache.org/guides/mini/guide-configuring-maven.html. – luka5z Dec 28 '16 at 16:20
  • 1
    @luka5z: Thanks for the important doc ref, but I can say with experience the path can be relative for `--define maven.repo.local=local/repo/here`. Maybe absolute paths are only required for `settings.xml`? – kevinarpe Dec 23 '18 at 14:29
  • @Raghuram: flawless answer – Gaurav Jan 22 '19 at 06:43
19

Use the localRepository setting in your settings.xml file. Example:

<settings>
  <localRepository>/repo</localRepository>
  ...
</settings>

See here for more info.

You can also set the repository via the command line using "-Dmaven.repo.local=" such as:

mvn -U clean install -Dmaven.repo.local=C:\tmp
Michael
  • 6,141
  • 2
  • 20
  • 21
  • 1
    We have already done it in our settings.xml. I want to override it when I invoke maven in a shell – Anthony Kong Feb 03 '12 at 05:13
  • 1
    If maven is using that settings.xml, even when you invoke it from a shell, it should pick up that setting. It's very common for windows users to need to set that due to spaces in their home directory "c:\documents and settings\blah" – Michael Feb 03 '12 at 15:52
  • 1
    Just ran a test using " /Users/michael/Desktop/repository " in my settings.xml file. And now when I build anything from command line (or ide) it's putting the artifacts there. – Michael Feb 03 '12 at 15:58
  • 1
    Sorry, misread your question. The answer above is correct. You can use the "-Dmaven.repo.local=/alternate/repo/location" option on the commands. – Michael Feb 03 '12 at 17:41