Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 645
MeowCode Avatar asked Jun 06 '18 18:06

MeowCode


3 Answers

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

like image 150
Francois Marot Avatar answered Sep 18 '22 12:09

Francois Marot


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 "
    }
}
like image 43
idriss Eliguene Avatar answered Sep 20 '22 12:09

idriss Eliguene


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'
            }
        }
    }
}
like image 40
pan Avatar answered Sep 16 '22 12:09

pan