在Jenkins管道中使用Maven版本插件提升版本时出现错误的替换错误 [英] Getting bad substitution error when bumping up version using maven versions plugin in Jenkins pipeline
问题描述
在我的jenkins管道中运行此命令时,我收到一个严重的替换错误
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]运行shell脚本
[code] Running shell script
/apps/jenkins/latest/workspace/ess-holani_master-3YNVBB6LFQA3QFK5NHYV57DW5HGSNALVGFJTJ4D6T72QVPJG4CDA/code@tmp/durable-374bc417/script.sh: 第2行: -DnewVersion = $ {parsedVersion.majorVersion}.$ {parsedVersion.minorVersion}.$ {parsedVersion.nextIncrementalVersion}-快照: 错误的替换
/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
脚本返回了退出代码1
script returned exit code 1
但这可行->
sh 'mvn build-helper:parse-version versions:set \
-DnewVersion=\\\${parsedVersion.majorVersion}.\\\${parsedVersion.minorVersion}.\\\${parsedVersion.nextIncrementalVersion}-SNAPSHOT \
-DgenerateBackupPoms=false \
-DprocessAllModules \
-DgenerateBackupPoms=false'
此命令可以按预期提高我的pom版本.
This command bumps up my pom version as expected.
运行上述命令时登录jenkins shell
Logs on jenkins shell when I run the above command
[code]运行shell脚本
[code] Running shell script
mvn构建帮助器:解析版本:set'-DnewVersion = $ {parsedVersion.majorVersion}.$ {parsedVersion.minorVersion}.$ {parsedVersion.nextIncrementalVersion} -SNAPSHOT' -DgenerateBackupPoms = false -DprocessAllModules -DgenerateBackupPoms = false
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 ?
迈克尔·沃克斯(Michael Works)提出的答案.
Answer suggested by michael works .
我也尝试过
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.
推荐答案
这里的问题是,您使用的是解释器和编译器的三层,并且它们对字符串中的变量替换使用相同的语法:"$ {myvar}".
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}".
- 首先是Groovy,由于它不知道任何名为
parsedVersion.minorVersion
的变量,因此会给您带来不好的替代.您可以使用'
而不是"
来防止Groovy尝试尝试使用\
逃避$
.但是Groovy会将\$
作为$
传递给\\\$
和将\\\$
作为\$
传递给下一个实例. - 第二个是壳牌.同样,您可以选择使用
'
或转义. - Maven,这就是您要替换的
$
- 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. - Second there is the Shell. Again you have the option to use
'
or to escape. - 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"
或使用'
s的一个,因此我们需要对'
进行转义,但无需对$
s进行任何转义:
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'
这篇关于在Jenkins管道中使用Maven版本插件提升版本时出现错误的替换错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!