Search code examples
mavenjenkinsjenkins-pipelinejenkins-agent

Configure settings.xml in jenkins slave created on the fly in AWS


I am creating Jenkins Slave on the fly configuring it on AWS with Spot Instances. In the global tool configuration, I have set to use my own "settings.xml" for the master is working perfectly.

But when the server start slaves (without maven installed) it auto install maven (set in the Jenkins file to install this tool) but without putting any settings.xml

*I know I can copy the setting.xml directly from the server but for me looks like it is not the appropriate way to do it. * I already did mvn -X in order to see find the folder for the settings but this is not used.

Added one small slice of the jenkinsfile

pipeline {

tools{
    maven 'maven default'
}
agent  any

stages {
    stage('Maven build') {
        steps {
           sh 'mvn clean install'                
            }
        }
    }
}

Solution

  • You have to use withMaven() in the Pipeline code..which has to look like this:

    withMaven(jdk: jdkName, maven: MavenInGlobalToolsName, mavenSettingsConfig: 'IdInConfigFileProvided', mavenLocalRepo:".repository") {
      sh "mvn clean verify"
    }
    

    The IdInConfigFileProvided is the important part which makes the reference to the config file provider plugin...

    The other solution could be to use the config file provider directly in Jenkins file:

    configFileProvider(
        [configFile(fileId: 'maven-settings', variable: 'MAVEN_SETTINGS')]) {
        sh 'mvn -s $MAVEN_SETTINGS clean package'
    }
    

    But this solution will not handle the support global tools support for Maven itself. So I would suggest to prefer the withMaven() solution.