Getting bad substitution error when bumping up version using maven versions plugin in Jenkins pipeline

2.3k views Asked by At

I get a bad substitution error when I run this command in my jenkins pipeline

sh 'mvn build-helper:parse-version versions:set \
-DnewVersion=${parsedVersion.majorVersion}.${parsedVersion.minorVersion}.${parsedVersion.nextIncrementalVersion}-SNAPSHOT \
-DgenerateBackupPoms=false \
-DprocessAllModules \
-DgenerateBackupPoms=false'

This is the error message in this case -

[code] Running shell script

/apps/jenkins/latest/workspace/ess-holani_master-3YNVBB6LFQA3QFK5NHYV57DW5HGSNALVGFJTJ4D6T72QVPJG4CDA/code@tmp/durable-374bc417/script.sh: line 2: -DnewVersion=${parsedVersion.majorVersion}.${parsedVersion.minorVersion}.${parsedVersion.nextIncrementalVersion}-SNAPSHOT: bad substitution

script returned exit code 1

But this works ->

sh 'mvn build-helper:parse-version versions:set \
-DnewVersion=\\\${parsedVersion.majorVersion}.\\\${parsedVersion.minorVersion}.\\\${parsedVersion.nextIncrementalVersion}-SNAPSHOT \
-DgenerateBackupPoms=false \
-DprocessAllModules \
-DgenerateBackupPoms=false'

This command bumps up my pom version as expected.

Logs on jenkins shell when I run the above command

[code] Running shell script

mvn build-helper:parse-version versions:set '-DnewVersion=${parsedVersion.majorVersion}.${parsedVersion.minorVersion}.${parsedVersion.nextIncrementalVersion}-SNAPSHOT' -DgenerateBackupPoms=false -DprocessAllModules -DgenerateBackupPoms=false

Using double quotes also work here -

sh "mvn build-helper:parse-version versions:set \
-DnewVersion=\\\${parsedVersion.majorVersion}.\\\${parsedVersion.minorVersion}.\\\${parsedVersion.nextIncrementalVersion}-SNAPSHOT \
-DgenerateBackupPoms=false \
-DprocessAllModules \
-DgenerateBackupPoms=false"

I want to understand why do we need to add an extra '\\\' in this script ?

Answer suggested by michael works .

I also tried this

sh "mvn build-helper:parse-version versions:set \
-DnewVersion='\${parsedVersion.majorVersion}.\${parsedVersion.minorVersion}.\${parsedVersion.nextIncrementalVersion}-SNAPSHOT' \
-DgenerateBackupPoms=false \
-DprocessAllModules \
-DgenerateBackupPoms=false"

I removed \ before the closing ' at the end . This also worked.

1

There are 1 answers

3
Michael On BEST ANSWER

The issue here is that you are using three layers of interpreters and compilers and all use the same syntax for variable substitutions in strings: "${myvar}".

  1. First there is Groovy, which would give you a bad substitution since it does not know any variable named parsedVersion.minorVersion. You can use 's instead of "s to prevent Groovy to try that or escape the $ with a \. Groovy however will pass \$ as $ and \\\$ as \$ to the next instance.
  2. Second there is the Shell. Again you have the option to use ' or to escape.
  3. Maven and that's the one which you'd like to do the replacement $

I think you could go with less confusion, if you'd use ' around your version string:

sh "mvn build-helper:parse-version versions:set \
  -DnewVersion='\${parsedVersion.majorVersion}.\${parsedVersion.minorVersion}.\${parsedVersion.nextIncrementalVersion}-SNAPSHOT' \
  -DgenerateBackupPoms=false \
  -DprocessAllModules \
  -DgenerateBackupPoms=false"

EDIT: or this one using 's so we need to escape the 's but don't need to do any escaping on the $s:

sh 'mvn build-helper:parse-version versions:set \
  -DnewVersion=\'${parsedVersion.majorVersion}.${parsedVersion.minorVersion}.${parsedVersion.nextIncrementalVersion}-SNAPSHOT\' \
  -DgenerateBackupPoms=false \
  -DprocessAllModules \
  -DgenerateBackupPoms=false'