223

Is it possible to point to a specific settings file in order to override the default settings.xml being used by maven for a single command? Example:

mvn clean install -Dparam # -> pass specific settings file path as param to override default "home/.m2/settings.xml"
guilhebl
  • 8,330
  • 10
  • 47
  • 66

4 Answers4

420

You can simply use:

mvn --settings YourOwnSettings.xml clean install

or

mvn -s YourOwnSettings.xml clean install
khmarbaise
  • 92,914
  • 28
  • 189
  • 235
  • 7
    Do you mean the local cache ? Can be defined inside `settings.xml` or via command line `-Dmaven.repo.local=$HOME/.my/other/repository` – khmarbaise Mar 10 '16 at 08:53
  • 2
    @NeerajSinghChouhan I know it's a little late, but in Eclipse, open Window > Preferences > Maven > User Settings. Specify a different User Settings (settings.xml) file there, then click Update Settings. – Ben Ingle Jan 04 '18 at 16:34
  • Is this a one off use, or does it set that location permanently? – mal May 07 '18 at 14:07
  • `--settings` did not work for me, but `-s` did. Maven version `Apache Maven 3.6.1`. – Koray Tugay Aug 12 '19 at 12:09
  • 1
    I ran mvn install with a non default settings.xml option via -s and the artifacts were posted to the default .m2 repository and not the repo specified in -s. I then tried specifying a non existent settings file via -s and it complained that the file didn't exist. So maven appears to be recognizing that I am specifying a non default file, but choosing not to use it or not use it fully. – JohnC Feb 25 '20 at 19:53
  • 3
    You can define a global settings file via `-gs,--global-settings` and a user settings file via `-s`...In user settings file you can override things If I correctly understand your question. – khmarbaise Feb 25 '20 at 20:34
  • `--settings` not worked because it is defined wrong, it should have equals sign, e.g. `--settings=.mvn/settings.xml` – Ramiz Apr 21 '20 at 13:42
  • 1
    Same thing happened with me as well. The option suggested by @khmarbaise worked for me. If someone in the future comes across this problem. This is the correct option mvn -gs=/usr/local/Cellar/maven/3.6.3_1/libexec/conf/settings_.xml clean install – Gagan May 09 '20 at 14:00
  • use --global-settings=.mvn/settings.xml if you just need to override the local maven repository while still loading servers/mirrors/profiles etc from ~/.m2/settings.xml – caduceus Jan 27 '21 at 08:38
  • 1
    @khmarbaise settings=.mvn/settings.xml only works if .mvn is in user.dir. This use case demonstrates the need for a property that exposes the highest basedir without relying on plugins or even extensions. After discovering what it is (I see Maven will recursive up the directory tree to find .mvn), Maven simply needs to make it available, so that it can be used in --settings=${maven.basedir}/.mvn/settings.xml etc. Do you see any issues with this? – caduceus Feb 18 '21 at 13:06
  • 4
    The `settings.xml` is global not project based. So the location for `settings.xml` is your home directory in `$HOME/.m2/` nowhere else. Apart from that it could contain credentials which should never be put in your project. The `.mvn` directory is intended for extensions not for `settings.xml` file... – khmarbaise Feb 18 '21 at 13:11
  • 1
    @khmarbaise Do you know if the Maven settings file can be configured via an environment variable (instead of the `-s` option)? Such as `MAVEN_SETTINGS_FILE=/some/path/settings.xml`? – Seelenvirtuose Apr 22 '21 at 06:14
  • 1
    No it can not be configuration via environment variable. – khmarbaise Apr 22 '21 at 07:23
  • @Seelenvirtuose Please comment in english. – khmarbaise Apr 22 '21 at 08:36
11

I just find it very difficult, It was still not working for me. Then I checked the comments and understood that you have to override global settings. Pointed by @khmarbaise I am adding this in a new answer.

mvn -gs /local-path/settings.xml clean install

mvn --global-settings /local-path/settings.xml clean install
Aakarsh Gupta
  • 195
  • 1
  • 6
4

You may need to override both global and user settings. This worked for me:

mvn -s C:\settings.xml -gs C:\settings.xml clean install
0

If you are using Intellij IDEA, and using ".mvn/maven.config" file inside your project to override default settings.xml file used by maven, you must use FULL syntax inside "maven.config" file.

VALID "maven.config" file:

--settings=C:\path\to\conf\my_settings.xml
--global-settings=C:\path\to\conf\my_settings.xml

Running "mvn clean" command from IntelliJ terminal with INVALID "maven.config":

-s C:\path\to\conf\my_settings.xml
-gs C:\path\to\conf\my_settings.xml

[ERROR] Error executing Maven.
[ERROR] Illegal char <:> at index ...

This answer was derived from comments under khmarbaise answer, and was added as seperate answer for visibility.

xyman
  • 399
  • 3
  • 10