I want to replace VERSION
placeholders in a file to a variable version
value, but I'm running into the below error:
def versions = ["8.8.0", "9.9.0"]
versions.each { version ->
def file = new File("$Path/test.url")
def fileText = file.replaceAll("VERSION", "${version}")
file.write(fileText);
Error:
groovy.lang.MissingMethodException: No signature of method: java.io.File.replaceAll() is applicable for argument types: (java.lang.String, org.codehaus.groovy.runtime.GStringImpl) values: [VERSION, 8.8.0]
I'm a newbie to groovy dsl, not sure what I'm missing, any suggestions, appreciated !
Another way is to use the groovy file .text
property:
def f = new File('sample-file.txt')
f.text = f.text.replaceAll('VERSION', '8.8.0')
and like @cfrick mentioned, there is not much point in performing the replace operation on multiple versions as only the first one will actually find the VERSION
string.
Running the above on a sample file:
─➤ groovy solution.groovy
─➤
will result in the string being replaced:
─➤ diff sample-file.txt_original sample-file.txt
1c1
< Dolore magna aliqua. VERSION Ut enim ad minim veniam.
---
> Dolore magna aliqua. 8.8.0 Ut enim ad minim veniam.
where diff
is a linux tool for comparing two files.