What to do when Nexus returns '401'


November 24, 2010 By Juven Xu

Sonatype helps open source projects by providing free Maven repository hosting and Maven central repository sync. There are hundreds of projects using this great service. As a result, I’ve received many questions from users, and a large amount of them is about ’401′. It is sometimes the case that when people try to deploy artifacts into Nexus using Maven, the deployment fails and they receive a 401 error.

The general definition of ’401′ can be bound at the HTTP RFC.  Briefly speaking, a ’401′ error occurs when the server asks for user authentication but the client can not provide it.  In Nexus, this means the Nexus server asks you log in first before doing things like deploying artifacts. So when you get a 401 on deploying artifacts to Nexus, you need to make sure you provide correct credentials.

Here is a check list you can follow:

  • Make sure your username/password is correct by logging into the Nexus UI. If curl is installed on your machine, you can try deploying an artifact with a command like this:
curl -u juven-test:****** https://oss.sonatype.org/service/local/staging/deploy/maven2/com/
juvenxu/ossrh-test/1.1/ossrh-test-1.1.pom --request PUT --data @pom.xml
  • If there is no error output, ensure your user privileges are correctly configured on the server.  Make sure to drop the repo you just created.
  • Make sure you have configured a server in settings.xml and that the server id is identical to the distribution repository id in pom.xml. For example, this:
  • <project>
      ...
      <distributionManagement>
        <repository>
          <id>sonatype-nexus-staging</id>
          <name>Nexus Staging Repository</name>
          <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
        </repository>
      </distributionManagement>
      ...
    </project>
    

    requires

<settings>
  ...
  <servers>
    <server>
      <id>sonatype-nexus-staging</id>
      <username>your-jira-id</username>
      <password>your-jira-pwd</password>
    </server>
  </servers>
  ...
</settings>
  • Make sure your settings.xml is in the correct place (normally it’s _~/.m2/settings.xml).  You can check this by running mvn help:effective-settings.
  • If the server is using https but the URL in your POM is http, you might get 401 as well.
  • Use the latest version of Maven, as there is a known issue regarding 401: MNG-4469