Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-68235

withMaven is not picking global configuration defined under tools

      It seems that "withMaven" is not picking global configuration correctly when maven is defined under "tools" although logs indicate it otherwise. 

      Here is sample pipeline:

      pipeline {
          agent any
          tools {
              maven 'maven3'       //1
              jdk 'openjdk-11'
          }
          stages {
              stage('Not Ok') { 
                  steps {
                  	withMaven{       //2
              	    sh "mvn help:effective-settings"
              	}
                  }
              }
              stage('Ok') { 
                  steps {
                  	withMaven(maven: 'maven3'){        //3
              	    sh "mvn help:effective-settings"
              	}
                  }
              }
          }
      }
      

       

      I have defined my Global Settings settings.xml (under Managed Files) and it's configured under Global Tool Configuration.

      When you run the pipeline then at (2) in logs this can be found:

      [withMaven] using Maven global settings.xml '4a3dd40e-13fa-4d56-9a7d-6c2390e85aa2'  

      but maven effective settings does not print my settings.xml.

       

      However, when I explicitely define maven with (3)

      withMaven(maven: 'maven3')

      then effective settings include my settings.xml.

      Please check attached file.

      I would expect that it is enough do define maven once (1) to pick correct settings.xml.

       
       
       

          [JENKINS-68235] withMaven is not picking global configuration defined under tools

          Related source code:

          ModelInterpreter.toolsBlock examines the Tools instance, installs each tool listed there, and applies their environment variables. Maven.MavenInstallation.buildEnvVars(EnvVars env) adds the "M2_HOME" and "MAVEN_HOME" environment variables. WithMavenStepExecution2 then looks for those environment variables, so pipeline-maven-plugin should use the version of Maven that was referenced in the "tools" section of the pipeline. However, I don't understand why the Maven settings would depend on whether the version of Maven was deduced in this way or specified as a parameter of the "withMaven" step.

          Kalle Niemitalo added a comment - Related source code: https://github.com/jenkinsci/jenkins/blob/jenkins-2.332.2/core/src/main/java/hudson/tasks/Maven.java https://github.com/jenkinsci/pipeline-model-definition-plugin/blob/pipeline-model-definition-1.9.3/pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy#L511-L563 https://github.com/jenkinsci/pipeline-model-definition-plugin/blob/pipeline-model-definition-1.9.3/pipeline-model-definition/src/main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/model/Root.groovy#L50 https://github.com/jenkinsci/pipeline-model-definition-plugin/blob/pipeline-model-definition-1.9.3/pipeline-model-definition/src/main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/model/Tools.groovy https://github.com/jenkinsci/pipeline-model-definition-plugin/blob/pipeline-model-definition-1.9.3/pipeline-model-definition/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/steps/EnvVarsForToolStep.java https://github.com/jenkinsci/workflow-cps-plugin/blob/workflow-cps-2.94.1/src/main/java/org/jenkinsci/plugins/workflow/cps/CpsScript.java#L83-L123 https://github.com/jenkinsci/workflow-basic-steps-plugin/blob/workflow-basic-steps-2.24/src/main/java/org/jenkinsci/plugins/workflow/steps/ToolStep.java https://github.com/jenkinsci/pipeline-maven-plugin/blob/pipeline-maven-3.10.0/jenkins-plugin/src/main/java/org/jenkinsci/plugins/pipeline/maven/WithMavenStepExecution2.java#L457-L459 ModelInterpreter.toolsBlock examines the Tools instance, installs each tool listed there, and applies their environment variables. Maven.MavenInstallation.buildEnvVars(EnvVars env) adds the "M2_HOME" and "MAVEN_HOME" environment variables. WithMavenStepExecution2 then looks for those environment variables, so pipeline-maven-plugin should use the version of Maven that was referenced in the "tools" section of the pipeline. However, I don't understand why the Maven settings would depend on whether the version of Maven was deduced in this way or specified as a parameter of the "withMaven" step.

            Unassigned Unassigned
            mcwikal Mariusz Ćwikła
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: