Getting Project Version from Maven POM in Jenkins

前端 未结 11 2293
情歌与酒
情歌与酒 2020-12-01 03:19

Is there any way a Jenkins build can be aware of the Maven version number of a project after processing the POM?

I\'ve got some projects where versioning is controll

11条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-01 03:49

    I used Pipeline Utility Steps plugin in a declarative pipeline job to get Maven version. In the example below I use script variable instead of environment variable, because that can be modified and passed between stages.

    def TAG_SELECTOR = "UNINTIALIZED"
    pipeline {
        agent any
        stages {
            stage('Build') {
                steps {
                    sh "mvn --batch-mode -U deploy"
                    script {
                        TAG_SELECTOR = readMavenPom().getVersion()
                    }
                    echo("TAG_SELECTOR=${TAG_SELECTOR}")
                }
            }
        }
    }
    

    Note: You must approve the getVersion() method after creating the job in Manage jenkins > In-process Script Approval.

    See also:

    • readMavenPom documentation

提交回复
热议问题