I am getting an error "Return code is: 401, ReasonPhrase: Unauthorized." when trying to deploy artifacts to nexus through jenkins

11,543

Solution 1

You need to have a <distributionManagement/> section in your pom.xml file. Something like this:

<distributionManagement>
    <repository>
        <id>my-releases</id>
        <name>my-releases</name>
        <url>http://your-repository-host:8081/nexus/content/repositories/my-releases/</url>
        <layout>default</layout>
    </repository>
    <snapshotRepository>
        <id>my-snapshots</id>
        <name>my-snapshots</name>
        <url>http://your-repository-host:8081/nexus/content/repositories/my-snapshots/</url>
        <layout>default</layout>
    </snapshotRepository>
</distributionManagement>

Then you also need to have something like this in your settings.xml file:

<servers>
    <server>
        <id>my-releases</id>
        <username>your-username</username>
        <password>your-password</password>
    </server>
    <server>
        <id>my-snapshots</id>
        <username>your-username</username>
        <password>your-password</password>
    </server>
</servers>

Please, note that the <id/> sections in your <distributionManagement/> in your pom.xml should match those in your <server/> sections in your settings.xml file.

Also, your settings.xml file must (normally) reside under ~/.m2, unless you have defined thes in Jenkins.

Solution 2

Your Maven settings file should go either in the folder that Maven is installed in (globally available for all users) or in the home folder of the user that is executing the Jenkins jobs in $HOME/.m2/settings.xml.

You can call mvn -X | grep settings to see where Maven is looking for the settings files.

Solution 3

try to run deploy manually from the slave, if it's works make sure you are using the correct settings.xml by adding -x to your mvn command.

Solution 4

I had a similar situation that we solved; where we were getting a "401 unauthorized" during the "release" job.

While trying to debug it, we changed the jenkins/maven job from:

"clean release:clean release:prepare release:perform"

to

"clean deploy".

The "deploy" worked, so that gave us confidence in the general config/plumbing aspects of the job. But the "release" was still failing; all other aspects of the job configuration were identical (since we were just inline changing the goal and options)

enter image description here

To make it more complicated, in our case we dont hardcode credentials in the settings.xml file. Rather, those are injected by Jenkins at build/job time.

Add "NEXUS_USER" here. enter image description here

Add "NEXUS_PASS" here. enter image description here

Configure Jenkins/Maven to use your settings.xml file enter image description here

Add this to your pom.xml (should be no surprise here)

<distributionManagement>
    <repository>
        <id>com.xxx.repo_releases</id>
        <url>https://sonatype-nexus.xxx.com/releases/</url>
        <name>Internal releases repository</name>
    </repository>
    <snapshotRepository>
        <id>com.xxx.repo_snapshots</id>
        <url>https://sonatype-nexus.xxx.com/snapshots/</url>
        <name>Internal snapshots repository</name>
    </snapshotRepository>
</distributionManagement>

From the settings.xml (this file is a peer to the pom.xml in our git repo)

<servers>
  <server>
    <id>com.xxx.repo_releases</id>
    <username>${env.NEXUS_USER}</username>
    <password>${env.NEXUS_PASS}</password>
  </server>
  <server>
    <id>com.xxx.repo_snapshots</id>
    <username>${env.NEXUS_USER}</username>
    <password>${env.NEXUS_PASS}</password>
  </server>
</servers>

SOLUTION: It turns out the problem was the with the "maven-release-plugin", and that it could not interpolate the ${NEXUS_USER} in the settings.xml file. Once we bumped the version (from 2.1) to 2.5.3, it all worked.

enter image description here

Share:
11,543
Aklim Shaik
Author by

Aklim Shaik

Updated on June 28, 2022

Comments

  • Aklim Shaik
    Aklim Shaik almost 2 years

    I have my settings.xml file in 'u01/jenkins/.m2/'. Is this the place where i have to configure for nexus deployment? Please advice

    [ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy (default-deploy) on project jenktest: Failed to deploy artifacts: Could not transfer artifact tulsa.jenkins.test:jenktest:pom:0.0.1-20170905.090435-1 from/to snapshots (http://myhost:8081/nexus/content/repositories/snapshots): Failed to transfer file: http://myhost:8081/nexus/content/repositories/snapshots/tulsa/jenkins/test/jenktest/0.0.1-SNAPSHOT/jenktest-0.0.1-20170905.090435-1.pom. Return code is: 401, ReasonPhrase: Unauthorized. -> [Help 1]
    org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy (default-deploy) on project jenktest: Failed to deploy artifacts: Could not transfer artifact tulsa.jenkins.test:jenktest:pom:0.0.1-20170905.090435-1 from/to snapshots (http://204.26.165.206:8081/nexus/content/repositories/snapshots): Failed to transfer file: http://myhost:8081/nexus/content/repositories/snapshots/tulsa/jenkins/test/jenktest/0.0.1-SNAPSHOT/jenktest-0.0.1-20170905.090435-1.pom. Return code is: 401, ReasonPhrase: Unauthorized.