bish
bish

Reputation: 3419

Increment Maven project.version using build-helper-plugin in Jenkins pipeline

You can use the maven-build-helper-plugin to parse the version and then use the maven-version-plugin to set new versions (see this blog):

clean build-helper:parse-version versions:set -DnewVersion=${parsedVersion.majorVersion}.${parsedVersion.minorVersion}.${parsedVersion.nextIncrementalVersion} versions:commit

This works fine when the command is executed as "maven goal"-prebuild-step in a Maven-job.

Now I'm trying to convert all my Maven-job to pipeline-jobs.

withMaven(
  // Maven-Installation
  maven: "${MavenHelper.MAVEN3D3D9}") {

  String command = 'mvn build-helper:parse-version versions:set -DnewVersion=${parsedVersion.majorVersion}.${parsedVersion.minorVersion}.${parsedVersion.nextIncrementalVersion} versions:commit -f ' + komponente.getPomPath()

  sh(command)      
} 

This always gives me a bad substitution error as the shell script tries to parse these variables. But in this context the variables are filled by the maven-build-helper-plugin during execution.

DnewVersion=${parsedVersion.majorVersion}.${parsedVersion.minorVersion}.${parsedVersion.nextIncrementalVersion}: bad substitution

I already tried to escape them via DnewVersion=\${parsedVersion.majorVersion}.... but still get the same error.

Any advice to get it working without incrementing it manually before passing it to the version-plugin.

Upvotes: 3

Views: 3566

Answers (1)

wirnse
wirnse

Reputation: 1146

The following is working:

pipeline {
    agent any
    tools {
        maven 'Maven 3.6.0'
    }
    stages {
        stage('Change Version') {
            steps {
                sh 'mvn build-helper:parse-version versions:set -DnewVersion=\\${parsedVersion.majorVersion}.\\${parsedVersion.minorVersion}.\\${parsedVersion.nextIncrementalVersion}'
                sh "mvn build-helper:parse-version versions:set -DnewVersion=\\\${parsedVersion.majorVersion}.\\\${parsedVersion.minorVersion}.\\\${parsedVersion.nextIncrementalVersion}"
            }
        }
    }
}

Upvotes: 3

Related Questions