ronin667
ronin667

Reputation: 423

Jenkins declarative pipeline and custom maven settings.xml

I have a maven project that resolves some of its dependencies from a private repository (Nexus) inside the company network. Therefore it can only be build using a custom settings.xml where credentials for the private repository are stored.

The maven project is built by Jenkins using a declarative pipeline. The build job is a multibranch pipeline.

In the job configuration in Jenkins, under "Pipeline Maven Configuration", I specified both a settings file (managed file) and a global settings file.

However, the pipeline itself seems to completely ignore this setting.

It seems for the custom settings.xml to be used, I have to wrap every single mvn call into either a withMaven() {...} block or a configFileProvider () {...} block.

When I do this, it's working fine, but since there are a lot of mvn calls in this pipeline, this would make the pipeline unnecessarily complex.

Is there another way of making maven pick up the custom settings.xml file?

What's the point of the "Pipeline Maven Configuration" setting anyway if the specified files aren't used without further configuration inside the pipeline?

Upvotes: 3

Views: 3802

Answers (2)

Paul Verest
Paul Verest

Reputation: 63912

Other way is using Pipeline Maven Integration

https://github.com/jenkinsci/pipeline-maven-plugin

node {
  stage ('Build') {

    git url: 'https://github.com/cyrille-leclerc/multi-module-maven-project'

    withMaven(
        // Maven installation declared in the Jenkins "Global Tool Configuration"
        maven: 'maven-3', // (1)
        // Use `$WORKSPACE/.repository` for local repository folder to avoid shared repositories
        mavenLocalRepo: '.repository', // (2)
        // Maven settings.xml file defined with the Jenkins Config File Provider Plugin
        // We recommend to define Maven settings.xml globally at the folder level using
        // navigating to the folder configuration in the section "Pipeline Maven Configuration / Override global Maven configuration"
        // or globally to the entire master navigating to  "Manage Jenkins / Global Tools Configuration"
        mavenSettingsConfig: 'my-maven-settings' // (3)
    ) {

      // Run the maven build
      sh "mvn clean verify"

    } // withMaven will discover the generated Maven artifacts, JUnit Surefire & FailSafe & FindBugs & SpotBugs reports...
  }
}

Upvotes: 0

leftbit
leftbit

Reputation: 838

At the moment I'm going with the following approach...

Placed my maven user settings file right next to the Jenkinsfile in my SCM. Declared the Maven to use in the "tools" section of the jenkinsfile:

  tools {
    maven 'Maven 3.6.3'
    jdk 'AdoptOpenJDK_8u222'
  }

Then I added the user settings to every maven call using the "-s" command line option:

  steps {
    sh 'mvn pmd:pmd -s usersettings.xml'
  }

Still not happy about repeatedly adding the user settings, but leaner than running "withMaven()" - and faster.

Update It is possible to wrap the Maven call with a Groovy function like this

def mvn(String cmd) {
    sh "mvn ${cmd} -s usersettings.xml"
}

to declutter the Pipeline script:

steps {
  mvn 'pmd:pmd'
}

Upvotes: 2

Related Questions