Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set encoding for filter() call in gradle copy task

Let's say there is this sample groovy code in my build.gradle:

import org.apache.tools.ant.filters.ReplaceTokens

task doSomeFiltering(type: Copy) {
    from 'some/directory'
    into 'some/target'

    filter(ReplaceTokens, tokens: [SAMPLE: '123abc456'])
}

If I had some Unicode characters in the copied and filtered files, they would be turned into the system default encoding in the output files, which creates some big problems if unicode-specific characters are used and the files are expected to stay in unicode.

So the problem is: This sample code does not respect custom encoding choices, it will always default to the system default for outputting the files after filtering. How can I set the encoding of the Reader/Writer that the filter uses?

Is it possible to work around this limitation, e.g. call Apache Ant directly?

like image 707
randers Avatar asked Sep 12 '15 20:09

randers


1 Answers

Specifying the encoding for filter calls was not natively supported by Gradle prior to version 2.14. Since Gradle 2.14, you can use the filteringCharset attribute of the Copy task, like this:

import org.apache.tools.ant.filters.ReplaceTokens

task doSomeFiltering(type: Copy) {
    from 'some/directory'
    into 'some/target'

    filteringCharset = 'UTF-8'

    filter(ReplaceTokens, tokens: [SAMPLE: '123abc456'])
}
like image 94
randers Avatar answered Sep 30 '22 17:09

randers