7

I have a project that needs to fetch artifacts from two different repos. And even the artifacts inside the repos have dependencies to one another. For example, my project might depend on artifact A (in repo 1), which depends on artifact B( in repo 2), which again depends on artifact C (in repo 1 again).

I am having a lot of trouble configuring my settings.xml to make it work (For example mvn always tries to go to repo 1 for artifact B, which is wrong). Anybody got a clue how exaclty this is decided in maven?

CuriousMind
  • 15,168
  • 20
  • 82
  • 120
  • I think your answer should be somewhere here: https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html ? – SiKing May 14 '15 at 17:23
  • How do you have configured your settings.xml ? – khmarbaise May 14 '15 at 17:33
  • 1
    @khmarbaise Whatever I have done is definitely wrong. I am curious in general how does maven solve the multiple repository problem – CuriousMind May 14 '15 at 17:35
  • 1
    I don't think you can control that. Maven will search in the repositories in the order that you define them in the settings.xml, until it finds what it needs. – Adarsh Oct 03 '20 at 00:36
  • But it doesn't! It looks in repo 1, doesn't find it, and then gives up, and never even attempts looking at repo 2 as far as I can tell. What gives? – Mark VY Oct 03 '20 at 01:35
  • And things are clearly more complicated than "just look in settings.xml" or else there wouldn't be people writing blog posts like this one: https://blog.sonatype.com/2009/02/why-putting-repositories-in-your-poms-is-a-bad-idea/ – Mark VY Oct 03 '20 at 03:06
  • There already appears to be an accepted answer which describe repository ordering https://stackoverflow.com/questions/5325407/how-to-set-order-of-repositories-in-maven-settings-xml – MarkAddison Oct 07 '20 at 14:35

1 Answers1

3

All repositories are searched in order.

The number one reason why this does not happen are <mirror>s. Mirrors override repository definitions. If you, for example, define a mirror with <mirrorOf>*</mirrorOf> it will take all the requests and your other repository definitions just don't matter any more.

The number two reason is problems with one of the repositories. A repository which does not contain an artifact is expected to answer with 404. If it instead returns some gibberish or a 500 error or anything else, this might break the resolution.

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
  • When you say "in order", what order is this? The one in settings.xml? Or does the pom affect things too? (assume no mirrors) – Mark VY Oct 04 '20 at 15:14
  • That I don't know. One more remark: Inside companies, you usually don't put multiple repositories into a `settings.xml` but let your Nexus or Artifactory do the heavy lifting. – J Fabian Meier Oct 04 '20 at 16:25
  • Well, it turns out that in my company, we have several Nexus servers, some of which manage multiple repos. Last question: according to your explanation, it seems that if there are no games with mirrors, and no misbehaving servers, then removing repositories should never help. At most it saves a bit of time and network traffic, but it can never make something findable if it wasn't already findable. Right? – Mark VY Oct 04 '20 at 17:19
  • That would be my understand, yes. For a company my advice would be to proxy the other Nexus so that your `settings.xml` only needs one. But I understand that you probably cannot change that. – J Fabian Meier Oct 04 '20 at 18:59
  • Thanks! Then I wonder what on earth is going wrong... – Mark VY Oct 04 '20 at 19:45
  • Maybe you can ask a new question containing the settings.xml. – J Fabian Meier Oct 05 '20 at 06:18
  • Doubt that I'm allowed to post that thing online. – Mark VY Oct 05 '20 at 15:48
  • Then you might construct a comparable dummy version. – J Fabian Meier Oct 05 '20 at 16:06
  • I just tried turning on more verbose logging, and it appears that you might be right after all. It looks like they all are getting tried after all. Thank you! – Mark VY Oct 05 '20 at 17:31
  • 1
    Okay things are finally making much more sense. There are some artifacts that are in both repos, but the pom files don't match! So I end up with a totally different dependency tree depending on which pom I pick up. Arrrggg! – Mark VY Oct 07 '20 at 16:15