49

I have a general settings.xml file for all Maven projects where a default profile and servers and mirrors are defined. How can I use project-specific settings to ignore the default?

Can I point to a local settings.xml file for one specific Maven project?

Passing a local settings file with --settings parameter also works but isn't there a way to do this permanently?

Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
Vincent
  • 6,058
  • 15
  • 52
  • 94
  • 1
    You can of course point to a specific settings.xml but usually this should be prevented cause it will follow the path having a settings.xml for each project which leads to failures..so you should check why you need different settings.xml for different projects? – khmarbaise Apr 01 '17 at 12:33
  • "cause it will follow the path having a settings.xml for each project which leads to failures" What do you mean by this? I'll follow your advice and try to solve with 1 settings file and maybe different profiles? Can you choose a profile in your project pom? – Vincent Apr 01 '17 at 12:38
  • The problem is simply if you have different things in your settings which are project specific this sounds wrong...and I made the experience it is wrong. Using different profiles is following the same path...The question is why do you need different settings and what kind of information do you have in your settings.xml which is different for your projects? – khmarbaise Apr 01 '17 at 12:48
  • @khmarbaise. The main project has dependencies hosted on a remote server. * so everyting is routed to that server. Now when I make use of dependencies which aren't on the server it tries looking for them on that server too. Also some repositories and pluginRepositories defined. – Vincent Apr 01 '17 at 12:53
  • Every project should use a central installed repository manager otherwise this will become hard to follow ...so simplest solution is having only a single settings.xml which routes to a company repository manager and everything which is needed is on that repository that's it... – khmarbaise Apr 01 '17 at 13:04
  • The repository must contain all of the desired artifacts, or be able to proxy the requests to other repositories. This setting is most useful when using an internal company repository with the Maven Repository Manager to proxy external requests. --> My case is you don't have access to the internal company repository and it's not configured to proxy (lot's of dependencies not found). I could just remove the mirror in the settings.xml but than you might have the case that some internal artifacts aren't found – Vincent Apr 01 '17 at 15:04
  • 1
    @Vincent You can proxy everything but one repository (using "*,!specialrep" in the mirror settings). Then you configure that other repository in your settings.xml. Now you have access to both worlds. – J Fabian Meier Apr 01 '17 at 15:59
  • I don't think that will work 100% because the specialrep is defined as "central" --> so all dependencies will be get publicly and the custom repo isn't checked first. I'll try and change the id of the custom repo --> than your solution should work – Vincent Apr 01 '17 at 16:16

5 Answers5

72

I think I have a solution to that problem, you need a recent maven version, I'm using maven 3.5.2 but the feature was introduced in maven 3.3.1 I believe, not sure.

The idea is to use the local .mvn folder (in the parent project's folder) where it is possible to configure several things like JVM options, maven options that are always used, etc.

For maven options just create a file maven.config inside the .mvn folder with the content --settings ./.mvn/local-settings.xml, and that should be about it. Of course the local-settings.xml should be a valid maven settings file.

Here's the structure within the maven project root folder :

parent-mvn-project
   ├── .mvn
   │   ├── local-settings.xml
   │   └── maven.config
   ├── submodule-A (if any submodules)
   └── submodule-B (if any submodules)
bric3
  • 40,072
  • 9
  • 91
  • 111
  • 1
    Is there any way to make Maven always refer to this local-settings.xml file .Without the mvn -S command. – Bhuvaneshwari May 22 '18 at 08:47
  • @Bhuvaneshwari I'm not sure what you mean exactly, that's what this answer is supposed to explain, i.e. passing automatically the local settings file upon `mvn` execution of this project. Or are you asking to make it default for any maven project ? (in which case I don't think that's possible at this time.) – bric3 May 22 '18 at 08:55
  • when I try to do a mvn install for a project in this structure , it only refers to the regular settings.xml in the .m2 and not the one in the local folder. What am I missing here – Bhuvaneshwari May 22 '18 at 08:59
  • 2
    @Bhuvaneshwari Does ythe workstation run the maven version supporting this feature? Is the `.mvn` at the root of the maven project? Does the file `maven.config` contains `--settings ./.mvn/local-settings.xml` ? – bric3 May 23 '18 at 19:22
  • I had missed the maven.config . Now able to run . Thank you @Brice – Bhuvaneshwari May 24 '18 at 09:00
  • @Brice Do you know if this `.mvn` folder is recognized by eclipse when updating references, etc.? Part of my idea is that I want to have a project based repository, so any project I create is not mixing things with the global one until I want to. – jlanza Aug 17 '18 at 16:34
  • @jlanza I have no idea if eclipse or netbeans understand the `.mvn` folder mechanism, which serves various purpose, like maven extensions. My bet is Eclipse (plugin?) probably don't support these as I believe it's is using their own code to handle maven project, so it probably don't have any _recent_ maven feature. IntelliJ IDEA uses the actual maven programmatically, it is possible to choose the embedded maven which may be outdated, or any other maven binary distribution on the `PATH` or not. – bric3 Aug 17 '18 at 21:37
  • @Brice Does the contents of `local-settings.xml` get merged with user-specific and global settings? – Pavel S. Mar 17 '19 at 12:03
  • 3
    I love this answer! For the record, the `.mvn` "project folder" is described in [Configuring Apache Maven](https://maven.apache.org/configure.html). Also, I have taken a leaf out of [Configuring your Local Repository](http://maven.apache.org/guides/mini/guide-configuring-maven.html) to set up a gitignored local Maven repository inside the `.mvn` directory, so now I have a completely sandboxed environment, which makes me very happy. – Dawngerpony Jun 07 '19 at 15:01
  • I think in combination with IntelliJ it is still necessary to follow instructions by p3quod. https://stackoverflow.com/a/56298222/6853900 – Domi W Nov 18 '19 at 16:09
  • 4
    This works only as long as you run mvn in project root, if you change directory maven will resolve the location of local_settings.xml relative to the current working directory. There is a bug filled against maven for this behavior, but no fix in sight: https://issues.apache.org/jira/browse/MNG-6762 – Samo Dadela Nov 19 '20 at 17:07
  • @SamoDadela The correct way to build a submodule is to do that from the parent by using `mvn -pl the-module`, it may have worked from subfolders, I did use that until I understood it was wrong. – bric3 Apr 10 '22 at 09:17
  • I just read the linked ticket comments it also seem to go into that direction, see this [comment](https://issues.apache.org/jira/browse/MNG-6762?focusedCommentId=17425105&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-17425105). – bric3 Apr 10 '22 at 09:19
9

In IntelliJ you can set a different settings file for any project:

  1. Go to Settings -> Build, Execution, Deployment -> Build tools -> Maven
  2. Set the user settings file to local-settings.xml
p3quod
  • 1,449
  • 3
  • 13
  • 15
7

After maven 3.3.1, use the project-settings-extension to load the project settings, and put project specific mirrors into ${basedir}/.mvn/settings.xml in each project. 

in ${basedir}/.mvn/extensions.xml

    <extensions xmlns="http://maven.apache.org/EXTENSIONS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/EXTENSIONS/1.0.0 http://maven.apache.org/xsd/core-extensions-1.0.0.xsd">
      <extension>
        <groupId>com.github.gzm55.maven</groupId>
        <artifactId>project-settings-extension</artifactId>
        <version>0.1.1</version>
      </extension>
    </extensions>

in ${basedir}/.mvn/settings.xml

  <settings>

    <mirrors>
      <mirror>
        <id>id</id>
        <url>https://url-for-this-project/</url>
        <mirrorOf>central</mirrorOf>
      </mirror>
    </mirrors>

    <profiles>
      <!-- profiles for this project, such as corp internal repositories -->
    </profiles>

  </settings>
James Z.M. Gao
  • 516
  • 1
  • 8
  • 13
  • Best plugin ever. Until maven support per-project settings.xml this is the only way to have projectDir/.m2/settings.xml. Thanks for your work! – Samo Dadela Nov 19 '20 at 17:09
  • per-project settings.xml was support years ago, but had been dropped from v3. – James Z.M. Gao Nov 20 '20 at 02:41
  • 1
    Be aware that the `project-settings-extension` plugin contruct an in memory settings by combining what's in the `~/.m2/settings.xml` and what's in the `.mvn/settings.xml`. The impact is that the `.mvn/settings.xml` can only **ADD** stuffs to what is already declared in the `~/.m2/settings.xml`. In no way the `.mvn/settings.xml` will replace what is already declared in `~/.m2/settings.xml`. – Zartc Mar 26 '21 at 18:27
  • @Zartc for most fields, the merge logic is same as merging user/global settings, and the project settings take higher priority. can you describe the detail of your case? – James Z.M. Gao Mar 27 '21 at 09:10
1

Just a sample settings.xml file in case anyone needs it.

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>${user.home}/personal-wks/.m2/repository</localRepository>
<interactiveMode>true</interactiveMode>
<offline>false</offline>

change your <localRepository> accordingly.

Refer to this answer to set up the project specific seetings.xml.

detailed blog on baeldung

AkSh
  • 218
  • 1
  • 7
1

You may set repositories in the Maven project pom.xml file, and this is one option suggested by its official website https://maven.apache.org/guides/mini/guide-multiple-repositories.html.

Here is an example.

<project>
...
  <repositories>
    <repository>
      <id>my-repo1</id>
      <name>your custom repo</name>
      <url>http://jarsm2.dyndns.dk</url>
    </repository>
    <repository>
      <id>my-repo2</id>
      <name>your custom repo</name>
      <url>http://jarsm2.dyndns.dk</url>
    </repository>
  </repositories>
...
</project>
Jason
  • 66
  • 4