Build

Jenkins 1.5.18 not deploying to nexus repository – Unauthorized (401)

I got into this error while setting up Jenkins to deploy a built artifact to a maven repository ( sonatype nexus ) on my Windows 7 system.

Uploading: http://localhost:8081/nexus/content/repositories/pippo/it/pigiuz/test/AutoDeploy/TestLibOne/1/TestLibOne-1.pom
ERROR: Failed to deploy artifacts: Could not transfer artifact it.pigiuz.test.AutoDeploy:TestLibOne:swc:1 from/to pippo (http://localhost:8081/nexus/content/repositories/pippo/): Failed to transfer file: http://localhost:8081/nexus/content/repositories/pippo/it/pigiuz/test/AutoDeploy/TestLibOne/1/TestLibOne-1.swc. Return code is: 401, ReasonPhrase: Unauthorized.
org.apache.maven.artifact.deployer.ArtifactDeploymentException: Failed to deploy artifacts: Could not transfer artifact it.pigiuz.test.AutoDeploy:TestLibOne:swc:1 from/to pippo (http://localhost:8081/nexus/content/repositories/pippo/): Failed to transfer file: http://localhost:8081/nexus/content/repositories/pippo/it/pigiuz/test/AutoDeploy/TestLibOne/1/TestLibOne-1.swc. Return code is: 401, ReasonPhrase: Unauthorized.

It turned out that the default installation of Jenkins runs as SYSTEM not as <USER>, this means that the M2_HOME is not resolved in the current user .m2 folder and settings.xml is picked up from the maven installation folder.
To solve this issue Jenkins provides you an option under the “advanced options” menu at the “build” label to specify where the settings.xml is located.

here’s a screenshot of my configuration:

specifying_maven_settings_jenkins

The specified settings.xml has to contain the credentials to deploy the built artifact to the wanted server. If you didn’t change the default nexus deployment user password the configuration looks similar to this:

<server>
<id>myrepoID</id>
<username>deployment</username>
<password>deployment</password>
</server>

You can find maven settings.xml file reference hereĀ http://maven.apache.org/settings.html