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

不羁岁月 提交于 2019-12-24 11:37:26

问题


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:


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'


来源:https://stackoverflow.com/questions/52399604/getting-bad-substitution-error-when-bumping-up-version-using-maven-versions-plug

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!