Gradle String Replacement - no placeholders

2019-01-22 07:20发布

问题:

Is it possible to do simple string replacement in gradle where placeholders / tokens can not be used.

For example: given temp.txt replace all occurences of xxx with yyy.

回答1:

I assume you are talking about resource processing. In that case, you can use the free-form filter method:

processResources {
  filter { String line -> line.replace(...) }
}


回答2:

Read the text in:

String contents = new File( 'whatever.txt' ).getText( 'UTF-8' ) 

Replace the text

contents = contents.replaceAll( 'xxx', 'yyy' )

Write the text out again

new File( 'replaced.txt' ).write( contents, 'UTF-8' )

You should be able to wrap them into a task and call the task as normal



回答3:

To replace in-place:

ant.replaceregexp(file: fout, flags: "g",
      match: 'schemaLocation="[^"]+/', replace: 'schemaLocation="', encoding: 'UTF-8')

Reference for syntax:

  • https://ant.apache.org/manual/Tasks/replace.html
  • https://ant.apache.org/manual/Tasks/replaceregexp.html


标签: groovy gradle