105

I am using Maven 3.0, and my .m2 folder location is C:\Users\me\.m2.

However, I do not have write access to that folder, but I want to change the repository location from the one defined in the settings.xml.

Due to restricted access, I am not able to edit the settings.xml to change the repository location.

How can I override the values of my settings.xml -or change the default location of the .m2 folder- without editing my C:\Users\me\.m2\conf\settings.xml file?

Sled
  • 18,541
  • 27
  • 119
  • 168
Patan
  • 17,073
  • 36
  • 124
  • 198
  • 2
    You can use -gs option in command line to set settings.xml path (http://books.sonatype.com/mvnref-book/reference/running-sect-options.html) – yodamad May 20 '13 at 12:37
  • @yodamad. Thank you for the reply. I am getting error as "No goals have been specified". My objective is to make sure that for every build, it should take custom setings.xml, not for a particular build. Can you tell me how to achieve that. – Patan May 20 '13 at 13:14
  • I am running the script files, hence I want to make it common for all. – Patan May 20 '13 at 13:19
  • It's also possible from the command line, ex: https://stackoverflow.com/questions/6823462/specifying-mavens-local-repository-location-as-a-cli-parameter – rogerdpack Nov 07 '18 at 22:41

6 Answers6

121

You need to add this line into your settings.xml (or uncomment if it's already there).

<localRepository>C:\Users\me\.m2\repo</localRepository>

Also it's possible to run your commands with mvn clean install -gs C:\Users\me\.m2\settings.xml - this parameter will force maven to use different settings.xml then the default one (which is in $HOME/.m2/settings.xml)

rogerdpack
  • 62,887
  • 36
  • 269
  • 388
Petr Mensik
  • 26,874
  • 17
  • 90
  • 115
  • Thank you for the answer. I am getting error as "No goals have been specified". My intention is to make sure that for every build, it should take custom setings.xml, not for a particular build. Can you tell me how to achieve that. – Patan May 20 '13 at 13:13
  • 5
    And I have already mentioned that I do not have write access to default .m2 repository. – Patan May 20 '13 at 13:14
  • If you want to set different `.m2` location globally, then you need to modify `$HOME/.m2/settings.xml`, I am not aware of any other way how to achieve this. – Petr Mensik May 20 '13 at 14:06
  • 1
    And for the first question, what is your command?Try the one in my asnwer, I have edited it. – Petr Mensik May 20 '13 at 14:07
  • For me this alone didnt work. I had to copy the settings.xml aswell into the default directory to get it working (C:\Users\MYNAME\.m2) – Snackaholic Oct 24 '19 at 08:26
  • 2023 still works :D – silentsudo May 31 '23 at 05:44
65

It's funny how other answers ignore the fact that you can't write to that file...

There are a few workarounds that come to my mind which could help use an arbitrary C:\redirected\settings.xml and use the mvn command as usual happily ever after.

mvn alias

In a Unix shell (or on Cygwin) you can create

alias mvn='mvn --global-settings "C:\redirected\settings.xml"'

so when you're calling mvn blah blah from anywhere the config is "automatically" picked up.
See How to create alias in cmd? if you want this, but don't have a Unix shell.

mvn wrapper

Configure your environment so that mvn is resolved to a wrapper script when typed in the command line:

  • Remove your MVN_HOME/bin or M2_HOME/bin from your PATH so mvn is not resolved any more.
  • Add a folder to PATH (or use an existing one)
  • In that folder create an mvn.bat file with contents:

    call C:\your\path\to\maven\bin\mvn.bat --global-settings "C:\redirected\settings.xml" %*
    

Note: if you want some projects to behave differently you can just create mvn.bat in the same folder as pom.xml so when you run plain mvn it resolves to the local one.

Use where mvn at any time to check how it is resolved, the first one will be run when you type mvn.

mvn.bat hack

If you have write access to C:\your\path\to\maven\bin\mvn.bat, edit the file and add set MAVEN_CMD_LINE_ARG to the :runm2 part:

@REM Start MAVEN2
:runm2
set MAVEN_CMD_LINE_ARGS=--global-settings "C:\redirected\settings.xml" %MAVEN_CMD_LINE_ARGS%
set CLASSWORLDS_LAUNCHER=...

mvn.sh hack

For completeness, you can change the C:\your\path\to\maven\bin\mvn shell script too by changing the exec "$JAVACMD" command's

${CLASSWORLDS_LAUNCHER} "$@"

part to

${CLASSWORLDS_LAUNCHER} --global-settings "C:\redirected\settings.xml" "$@"

Suggestion/Rant

As a person in IT it's funny that you don't have access to your own home folder, for me this constitutes as incompetence from the company you're working for: this is equivalent of hiring someone to do software development, but not providing even the possibility to use anything other than notepad.exe or Microsoft Word to edit the source files. I'd suggest to contact your help desk or administrator and request write access at least to that particular file so that you can change the path of the local repository.

Disclaimer: None of these are tested for this particular use case, but I successfully used all of them previously for various other software.

Community
  • 1
  • 1
TWiStErRob
  • 44,762
  • 26
  • 170
  • 254
  • 3
    Well, personally, I am here looking for ways to unobtrusively hijack installs for dynamically checked out projects, and I don't _want_ to edit the settings.xml. Additionally, if an organization allows users to bring their own device, or hires contractors, it may be better to force the use of a different settings.xml automatically, rather than relying on users to set it up correctly (and, for example, avoid accidental deployment of closed source jars to open source repositories). – Ajax Jan 30 '18 at 09:03
60

Nobody suggested this, but you can use -Dmaven.repo.local command line argument to change where the repository is at. In addition, according to settings.xml documentation, you can set -Dmaven.home where it looks for the settings.xml file.

See: Settings.xml documentation

Dragas
  • 1,140
  • 13
  • 29
  • 2
    [docs](https://maven.apache.org/ref/3.8.2/maven-model-builder/index.html) where `maven.repo.local` is mentioned. – Gunther Sep 02 '21 at 18:23
  • 1
    This is ideal for some CI/CD setups where you need to dynamically inject it. – el n00b Nov 23 '22 at 17:48
24

Below is the configuration in Maven software by default in MAVEN_HOME\conf\settings.xml.

<settings>
  <!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ~/.m2/repository
  <localRepository>/path/to/local/repo</localRepository>
  -->

Add the below line under this configuration, will fulfill the requirement.

<localRepository>custom_path</localRepository>

Ex: <localRepository>D:/MYNAME/settings/.m2/repository</localRepository>

Paramesh Korrakuti
  • 1,997
  • 4
  • 27
  • 39
9

You can change the default location of .m2 directory in m2.conf file. It resides in your maven installation directory.

add modify this line in

m2.conf

set maven.home C:\Users\me\.m2

Anshuman
  • 91
  • 1
  • 2
0

You can point to a different-settings.xml when you deploy your project. When deployed from the project folder you can have a relative path to get back to your home folder:

mvn clean deploy -s ../../.m2/different-settings.xml
Zon
  • 18,610
  • 7
  • 91
  • 99