Search code examples
mavenjenkinscredentialsmaven-deploy-plugin

Binding nexus credentials to maven deploy job in jenkins build


I have a maven pom project with defined distributionManagement section in pom.xml

<project>
...
    <distributionManagement>
        <repository>
            <id>my-repo</id>
            <url>http://nexus.my.local/repository/my-private</url>
        </repository>
    </distributionManagement>

</project>

in the settings.xml file I have set up the servers section

  <servers>
    <server>
      <id>my-repo</id>
      <username>${env.CI_DEPLOY_USERNAME}</username>
      <password>${env.CI_DEPLOY_PASSWORD}</password>
    </server>
  </servers>

in jenkins, I binded the credentials to these variables enter image description here But doing mvn clean deploy gives me following error

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy (default-deploy) on project XY:
Failed to deploy artifacts:
Could not transfer artifact XY:pom:1.0.0 from/to my-repo (http://nexus.my.local/repository/my-private):
Failed to transfer file: http://nexus.my.local/repository/my-private/XY-1.0.0.pom.
Return code is: 401, ReasonPhrase: Unauthorized. -> [Help 1]

When I echoed the variable ${CI_DEPLOY_USERNAME} in shell before build, it gave me **** output -> I think that is ok. Where else should I provide the variables? Is my project's settings.xml get used in maven deploy command?


Solution

  • Using environment variables as properties in Maven requires that they're prefixed with env., so it should be:

      <servers>
        <server>
          <id>my-repo</id>
          <username>${env.CI_DEPLOY_USERNAME}</username>
          <password>${env.CI_DEPLOY_PASSWORD}</password>
        </server>
      </servers>
    

    Reference:

    http://maven.apache.org/pom.html#Properties