The Gilbert Arenas Dagger
The Gilbert Arenas Dagger

Reputation: 12761

Maven artifact cannot be resolved; attempting to resolve artifact from the wrong repository

After upgrading a dependency in my pom.xml, I received the following error when attempting to download the new artifact (Using maven command line mvn spring-boot:run in this case).

The following artifacts could not be resolved: io.github.bonigarcia:webdrivermanager:jar:3.8.1, org.zaproxy:zap-clientapi:jar:1.7.0: Could not find artifact io.github.bonigarcia:webdrivermanager:jar:3.8.1 in spring-milestone (https://repo.spring.io/milestone)

The error message is correct, that artifact does not exist in https://repo.spring.io/milestone. It does exists in Maven central (https://repo.maven.apache.org/maven2), so why would Maven be looking for the artifact in the wrong repository?

I do know that this is tied to my Maven settings (settings.xml) where I define a mirror to our internal Nexus repository. When I remove the mirror, Maven successfully looks for and finds the artifact in Maven central. What I don't understand is the mechanics of how things work when an artifact does not exist in Nexus, and why this mirror setting changes how an artifact is discovered for the first time.

From settings.xml..

<mirrors>
    <mirror>
        <id>Nexus</id>
        <name>Nexus Public Mirror</name>
        <url>https://myinternalnexushost.net/nexus/content/repositories/central</url>
        <mirrorOf>central</mirrorOf>
    </mirror>
</mirrors>

It's also noteworthy that this is a Spring Boot project. I don't define any repositories in my pom.xml, but since I'm specifying a spring boot parent pom I'm inheriting the following repositories:

From Spring Boot parent pom

  <repositories>
    <repository>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <id>spring-milestone</id>
      <name>Spring Milestone</name>
      <url>https://repo.spring.io/milestone</url>
    </repository>
    <repository>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
      <id>spring-snapshot</id>
      <name>Spring Snapshot</name>
      <url>https://repo.spring.io/snapshot</url>
    </repository>
    <repository>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <id>rabbit-milestone</id>
      <name>Rabbit Milestone</name>
      <url>https://dl.bintray.com/rabbitmq/maven-milestones</url>
    </repository>
    <repository>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <id>central</id>
      <name>Central Repository</name>
      <url>https://repo.maven.apache.org/maven2</url>
    </repository>
  </repositories>

Upvotes: 2

Views: 2998

Answers (2)

papanito
papanito

Reputation: 2574

You usually define the repos under repositories and each of it has an identifier. This identifier is then used for the mirrors. In your config you mirror the repo with id central

<mirrors>
    <mirror>
        <id>Nexus</id>
        <name>Nexus Public Mirror</name>
        <url>https://myinternalnexushost.net/nexus/content/repositories/central</url>
        <mirrorOf>central</mirrorOf>
    </mirror>
</mirrors>

As the repo https://repo.spring.io/ has another identifier it is not mirrored using central. So you have to define mirrors for each other repos as well, for example

<mirrors>
    <mirror>
        <id>Nexus</id>
        <name>Nexus Public Mirror</name>
        <url>https://myinternalnexushost.net/nexus/content/repositories/central</url>
        <mirrorOf>central</mirrorOf>
    </mirror>
    <mirror>
        <id>Spring</id>
        <name>Nexus Public Mirror</name>
        <url>https://myinternalnexushost.net/nexus/content/repositories/spring-milestone</url>
        <mirrorOf>spring-milestone</mirrorOf>
    </mirror>
</mirrors>

See also the guidelines:

With Repositories you specify from which locations you want to download certain artifacts, such as dependencies and maven-plugins. Repositories can be declared inside a project, which means that if you have your own custom repositories, those sharing your project easily get the right settings out of the box. However, you may want to use an alternative mirror for a particular repository without changing the project files.

I generally would do the following which makes things a bit easier to maintain

  • create a "remote" repo for each of the required repos in Nexus
  • create a "group" repo in Nexus which contains all remote repos

then you have to specify only this in your settings.xml

<mirrors>
    <mirror>
        <id>Nexus</id>
        <name>Nexus Public Mirror</name>
        <url>https://myinternalnexushost.net/nexus/content/repositories/maven-group</url>
        <mirrorOf>*</mirrorOf>
    </mirror>
</mirrors>

Upvotes: 2

Powerlord
Powerlord

Reputation: 88846

When you tell Maven to use a mirror, that mirror is used instead of the original repository.

Which means when you mirror "central" you lose access to anything in Maven Central that's not in that mirror.

Upvotes: 2

Related Questions