Search code examples
jenkinsjenkins-pipelinejenkins-plugins

How to use pom version in shell command in jenkinsfile?


I am trying to get pom.xml version within Jenkinsfile and then use that version in a shell script. This gives the exception bad substitution when trying to use pom.version. What is the correct syntax to pass the pom version?

script {
    def pom = readMavenPom file: 'pom.xml'

    withCredentials(...) {

        sh '''#!/bin/bash                                        
              cp "${WORKSPACE}/target/stats-${pom.version}-shaded.jar" /xyz
        '''
    }                   
}

I installed Pipeline Utility Steps plugin for readMavenPom cmd but didn't restart Jenkins. Is it necessary to restart Jenkins?


Solution

  • If you want to substitute ${pom.version}, you need to use GString. The triple-quote you used defines a multiline Java string that does not support variables substitution. You need to replace ''' with """ to define a multiline GString that can substitute both variables, ${WORKSPACE} and ${pom.version}.