maven getting "Not Authorized" when trying to access nexus private repository

15,961

Oh my funky goat. The problem was that apparently in settings.xml, the Id field has to be the same as the one in the server field. ie:

<servers>
 <server>
      <id>nexus-release</id>          <---THIS MUST MATCH
      <username>USER_NAME</username>
      <password>USER_PASSWD</password>
  </server>
</servers>
<mirrors>
    <mirror>
      <id>nexus-release</id>          <---THIS
      <mirrorOf>*</mirrorOf>
      <url>http://MY_NEXUS_HOST:8081/nexus/content/groups/public</url>
    </mirror>
  </mirrors>

I guess it doesn't matter which one I use (they're both the same in this case, but that's not necessarily always true).

Share:
15,961
Kevin Milner
Author by

Kevin Milner

Developer/Devops at seven10 Storage. A living wikipedia for useless facts in a world that already has wikipedia. Moved from Oklahoma to the Boston area 10 years ago, would never go back. I ask lots of questions but don't have many answers.

Updated on June 11, 2022

Comments

  • Kevin Milner
    Kevin Milner almost 2 years

    I've set up a private nexus repo manager on an EC2 instance and followed the various instructions floating around the internet on how to set up a maven project to use it. I have also disabled the anonymous account. I am able to connect and copy repositories via curl -U username:password <the_url> and it seems to work fine. However when I try to use maven (any goals) The very first thing I get is

        [WARNING] Could not transfer metadata org.apache.maven.plugins:maven-compiler-plugin/maven-metadata.xml from/to nexus (http://MY_NEXUS_HOST:8081/nexus/content/groups/public): Not authorized , ReasonPhrase:Unauthorized.
    

    The mvn command then fails because it can't find the plugin anywhere. So the fact that I can use the rest command and it works as expected, but not through maven indicates to me that it is a problem with the configuration. I think I understand what's going on pretty well, and I've checked and rechecked the files, but I don't see anything wrong. Here's the settings.xml file

    <servers>
      <server>
          <id>nexus-snapshot</id>
          <username>USER_NAME</username>
          <password>USER_PASSWD</password>
      </server>
      <server>
          <id>nexus-release</id>
          <username>USER_NAME</username>
          <password>USER_PASSWD</password>
      </server>
    
    </servers>
    <mirrors>
        <mirror>
          <!--This sends everything else to /public -->
          <id>nexus</id>
          <mirrorOf>*</mirrorOf>
          <url>http://MY_NEXUS_HOST:8081/nexus/content/groups/public</url>
        </mirror>
      </mirrors>
      <profiles>
        <profile>
          <id>nexus</id>
          <!--Enable snapshots for the built in central repo to direct -->
          <!--all requests to nexus via the mirror -->
          <repositories>
            <repository>
              <id>central</id>
              <url>http://central</url>
              <releases>
                  <enabled>true</enabled>
              </releases>
              <snapshots>
                  <enabled>true</enabled>
              </snapshots>
            </repository>
          </repositories>
         <pluginRepositories>
            <pluginRepository>
              <id>central</id>
              <url>http://central</url>
              <releases>
                  <enabled>true</enabled>
                  <updatePolicy>never</updatePolicy>
              </releases>
              <snapshots>
                  <enabled>true</enabled>
                  <updatePolicy>always</updatePolicy>
              </snapshots>
            </pluginRepository>
          </pluginRepositories>
        </profile>
      </profiles>
      <activeProfiles>
        <!--make the profile active all the time -->
        <activeProfile>nexus</activeProfile>
      </activeProfiles>
    

    And here is the relevent portion of the pom file

        <distributionManagement>
            <repository>
                <id>nexus-release</id>
                <name>Nexus Release Repository</name>
                <url>http://MY_NEXUS_HOST:8081/nexus/content/repositories/releases</url>
            </repository>
            <snapshotRepository>
                <id>nexus-snapshot</id>
                <name>Nexus Snapshot Repository</name>
                <url>http://MY_NEXUS_HOST:8081/nexus/content/repositories/snapshots</url>
            </snapshotRepository>
        </distributionManagement>
    

    I'm wondering if there's a way to see the exact issue I'm running into. For instance, if I'm getting a 401, 403 or (for some reason?) 404. If someone can please help me I'd be ever so greatful. Oh, both maven and nexus are the latest versions as of last week. *edited because no matter how many times you check something before hitting submit...

  • labm0nkey
    labm0nkey almost 6 years
    I was fighting with this whole day and this answer is gold. The actual issue for me here was that the guides on some websites are wrong and have those id's different while they should be same.
  • NiVeK92
    NiVeK92 over 3 years
    I feel like the reasoning could be improved for this answer. This works because it tells maven to use the credentials defined inside the server tags for the mirror with the corresponding id. So if you want to give the mirror a different id e.g. "maven-central", you could do so by adding a "new server" inside the <servers> tags with id "maven-central" and the same credentials as above (if you host the repo on the same nexus instance). This also allows for hosting the repos on different servers with different credentials.
  • Piotr Michalczyk
    Piotr Michalczyk over 2 years
    @labm0nkey Also when a mirror id does not match any server id it means that when a mirror server is accessed no authorization will be used. Which might be perfectly fine if your mirror server doesn't require any username/password in order to download dependencies.