Jenkins Declarative Pipeline with custom settings.xml

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")


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


you have to declare and maven installation in your jenkins Managed Jenkins > Global Tools configuration and add maven installation named like M3.

declare a maven installation

After you have to registry your settings file :

manage jenkins > Managed files

And add your setting File

After this you can use the WithMaven function with your registry file like this:

steps {
    withMaven(maven: 'M3', mavenSettingsConfig: 'mvn-setting-xml') {
        sh "mvn clean install "
    }
}

Also possible, to use the secret file credentials from Credentials Binding Plugin

Create a secret file in jenkins: enter image description here

Then you can use this settings file like this

pipeline {
    environment {
        MVN_SET = credentials('maven_settings')
    }
    agent {
        docker 'maven:3-alpine'
    }
    stages {
        stage('mvn test settings') {
            steps {
                sh 'mvn -s $MVN_SET help:effective-settings'
            }
        }
    }
}

I had this issue all you have to do is add this small piece of code in your line

def mvnSettings = 'Location of the file'
sh "mvn clean install --settings ${mvnSettings} -P foo"

So now whenever maven runs it will locate the settings.xml file in the PATH that you specified

P.S. its a maven command which you can use to run on command Line

Hope it helps :)