Copy Task - Filtering With Token-less String Replacement

(Gunnar Hillert) #1


When using the copy task - How can I filter/expand and do pure String replacement, without using either Groovy’s SimpleTemplateEngine’s format ${stringToReplace} or Ant’s ReplaceToken format @StringToReplace@?

I basically have multiple files that need to be filtered using raw Strings, without marking those Strings with Tokens, e.g. a file may contain

“…lorem ipsum replace-this-string blubba foo bar…”

and ‘replace-this-string’ needs to be replaced with something else.

I saw some mentioning that I can use other ‘’ implementations and I guess maybe a custom one may be necessary. Anyway, I could not find any further examples for my use-case and I am admittedly still learning Gradle - So for any pointers I would be grateful.




(Peter Niederwieser) #2

One option is to use the free-form filter method:

task copy(type: Copy) {
  from ...
  into ...
  filter { String line -> line.reverse() }

Inside the closure you can do whatever is needed to make the replacement happen. For example you can use Java regular expressions.