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?
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'])
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With