简体   繁体   English

无法替换 Groovy 脚本中的字符串文本

[英]Cannot replace string text in Groovy script

I am trying to replace a test in pom.xml using a groovy script.我正在尝试使用 groovy 脚本替换pom.xml的测试。 These are my two approaches.这是我的两种方法。 The text should be replaced is {env.AM_$environment.toUpperCase()_SERVER_CREDS_USR}应该替换的文本是{env.AM_$environment.toUpperCase()_SERVER_CREDS_USR}

Approach one方法一

File mainPomXml = new File(rootDir,'/pom.xml')
mainPomXml.text.replace('{env.AM_$environment.toUpperCase()_SERVER_CREDS_USR}','${env.AM_$environment.toUpperCase()_SERVER_CREDS_USR}');

Approach two方法二

def mainPomXml = new File(rootDir,'/pom.xml')
def mainPom = mainPomXml.text.replace('{env.AM_$environment.toUpperCase()_SERVER_CREDS_USR}','${env.AM_$environment.toUpperCase()_SERVER_CREDS_USR}');
mainPomXml.write(mainPom);

But none of these approaches work.但是这些方法都不起作用。 That means both executes but the test is not get replaced.这意味着两者都执行但测试不会被替换。 How to fix this issue?如何解决这个问题?

Change the mainPom part as below.更改 mainPom 部分如下。

def mainPomXml = new File(rootDir, '/pom.xml')
def mainPom = mainPomXml.text.replace('AM_SERVER_CREDS_USR', '${env.AM_'+ env.toUpperCase() +'_SERVER_CREDS_USR}')
mainPomXml.write(mainPom)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM