Jenkins Declarative Pipeline with custom settings.xml

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


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'
            }
        }
    }
}

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