Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically escape unicode characters in Java property files using Gradle?

I'm translating a Java application by using a ResourceBundle with various *.properties files. Now I like to have a Gradle task or want to modify a task to automatically escape any unicode character by replacing it with its ASCII representation, something like Java's native2ascii tool does.

This is what I'm done so far with my build file, but the output remains unescaped:

import org.apache.tools.ant.filters.EscapeUnicode

tasks.withType(ProcessResources) {
    filesMatching('**/*.properties') {
        println "\t-> ${it}"
        filter EscapeUnicode
    }
}

Any help is appreciated.

like image 982
Christian Rädel Avatar asked Jun 09 '16 15:06

Christian Rädel


1 Answers

You can do it providing the additional copy specification for properties files, this way:

import org.apache.tools.ant.filters.EscapeUnicode
tasks.withType(ProcessResources).each { task ->
    task.from(task.getSource()) {
        include '**/*.properties'
        filter(EscapeUnicode)
    }
}
like image 168
Stanislav Avatar answered Oct 16 '22 11:10

Stanislav