Maven – Jenkins Declarative Pipeline with custom settings.xml

Jenkinsjenkins-pipelinemaven

I'm trying to set up a Jenkins Declarative Pipeline with maven. So far I can get maven to run, but I can't get it to use my defined Maven Settings.xml.

pipeline{
   agent any
   tools{
       maven 'Apache Maven 3.3'
       // without mavenSettingsConfig, my settings.xml is not used.  With it, this blows up
       mavenSettingsConfig: 'Global Maven Settings'
       jdk 'jdk9
   }
   stages {
       stage('Preparation'){
           steps{
              //code checkout stuff here--this works fine
           }
       }
       stage('Build'){
            steps{
               sh "mvn clean install -P foo"
            }
       }
   }
}

The problem seems to be mavenSettingsConfig. Without that property, I can't figure out how to set the settings.xml, and my custom maven stuff doesn't work. (Profile foo, for example.) With the mavenSettingsConfig, it blows up:

BUG! exception in phase 'canonicalization' in source unit 'WorkflowScript' unexpected NullpointerException….

The documentation has a big TODO in it where it would provide an example for this! So how do I do it?

(Documentation TODO at https://wiki.jenkins.io/display/JENKINS/Pipeline+Maven+Plugin. It actually says "TODO provide a sample with Jenkins Declarative Pipeline")

Best Answer

my advice is to use the Config File Provider plugin: https://wiki.jenkins.io/display/JENKINS/Config+File+Provider+Plugin

With it, you define your config file once in Jenkins' "Config File Management" screen and then have code like this in your pipeline:

stage('Build'){
    steps{
       configFileProvider([configFile(fileId: 'my-maven-settings-dot-xml', variable: 'MAVEN_SETTINGS_XML')]) {
            sh 'mvn -U --batch-mode -s $MAVEN_SETTINGS_XML clean install -P foo'
        }
    }
}

Hope it helps

Related Topic