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.
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}".
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.'
or to escape.$
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'