Reputation: 352
I have a node running Jenkins that builds code with maven. The Jenkins job is a declarative pipeline script. Maven needs to download dependencies from private repositories which require credentials to access. The credentials are stored in the Jenkins credential manager.
How can I pass these credentials to maven so that maven can correctly download dependencies from private repos using these credentials.
Upvotes: 9
Views: 19342
Reputation: 352
By injecting Jenkins credentials into your environment, and then passing those credentials to maven, you can access private repos using Jenkins credentials.
Steps:
withCredentials([usernamePassword(credentialsId: 'test-creds', passwordVariable: 'PASSWORD_VAR', usernameVariable: 'USERNAME_VAR')])
{
sh 'mvn clean install -Dserver.username=${USERNAME_VAR} -Dserver.password=${PASSWORD_VAR}'
}
<servers>
<server>
<id>ServerID</id>
<username>${server.username}</username>
<password>${server.password}</password>
</server>
</servers>
-s
or -gs
flag in your maven command.Upvotes: 11
Reputation: 11
from Config File Provider pageThis only works with freestyle or maven jobs. Will not work on multibranch pipeline. Refer to the config file provider plugin documentation here
Upvotes: 1
Reputation: 431
There is a better way how to solve this task using the Config File Provider and Credentilals Jenkins plugins. Just create the maven settings.xml config file and inject the server credentials into it.
Steps:
stage('Publish to nexus')
withMaven(mavenSettingsConfig: 'the name of your configuration file in jenkins'){
sh mvn clean deploy
}
Upvotes: 4