Gradle uses wrong encoding (Latin-1) for property file

1.9k views Asked by At

I try to read properties from a file with umlauts, this is my build.gradle:

task utf8test << {

    Properties props = new Properties()
    def propFile = new File("my.property")
    if (propFile.canRead()) {
        props.load(new FileInputStream(propFile))
        for (Map.Entry property in props) {
                println property.value
        }
    }
}

My property-file looks like (UTF-8 encoded):

challenge: ö

If i execute the task with: gradle utf8test The result looks like

:utf8test
ö

BUILD SUCCESSFUL

Total time: 0.877 secs

"ö" changes to "ö", which is easy to understand. "ö" as hexadecimal is c3b6 and c3 in latin-1 is à and b6 is ¶, but it is not what i expected.

Question: How can i configure gradle to read in properties as UTF-8 encoded

More Informations:

If i print out the propFiles content in gradle with:

println propFile.text

i receive "ö" as output, so the file is read in correctly and the output is encoded correctly by my shell.

Gradle-daemon runs with: -Dfile.encoding=UTF-8

Executing gradle with -Dfile.encoding=UTF-8:gradle utf8test -Dfile.encoding=UTF-8 does not help, nor does export GRADLE_OPTS="-Dfile.encoding=UTF-8" in bash, nor does adding systemProp.file.encoding=utf-8 to gradle.properties.

I was not able to find the documentation page for the Properties-Class in gradle, is there any option to configure the encoding?

Many thanks so far!

1

There are 1 answers

1
JB Nizet On BEST ANSWER

That's expected, and doesn't have much to do with gradle. The documentation of java.util.Properties (which has nothing to do with Gradle but is a standard class of the JDK) explicitely specifies that the standard encoding of properties file is ISO-8859-1. If you're the only one to read the file, and want it to contain UTF-8, then explicitely read it as UTF-8:

Properties props = new Properties()
def propFile = new File("my.property")
if (propFile.canRead()) {
    props.load(new InputStreamReader(new FileInputStream(propFile), StandardCharsets.UTF_8));
    for (Map.Entry property in props) {
            println property.value
    }
}