Show UTF-8 text properly in Gradle
Asked Answered
A

8

53

I have a task:

task info<<{
    println "Gradle — система автоматической сборки, построенная на принципах Apache Ant и Apache Maven, но предоставляющая DSL на языке Groovy вместо традиционной XML-образной формы представления конфигурации проекта."
}

when I use the command:

gradle info -Dfile.encoding=UTF-8

The text is not shown properly. How to fix it? Thanks.

Archfiend answered 21/1, 2014 at 19:23 Comment(0)
M
40

The file.encoding system property needs to be set right when the JVM executing the Gradle build (e.g. the Gradle Daemon) starts up. One way to achieve this is with export GRADLE_OPTS="-Dfile.encoding=utf-8". Another way that might work is to add systemProp.file.encoding=utf-8 to gradle.properties. Of course this assumes that the build script files are actually using utf-8 encoding. To see what your platform's (and therefore Gradle's) default encoding is, print out the system property's value in a build script.

Maunsell answered 21/1, 2014 at 19:47 Comment(9)
My System file.encoding=Cp1251. So if I change it on my machine to UTF-8 will this solve my problem in general?Archfiend
What matters is the Java platform encoding.Maunsell
OK .I add System Environment Variable on my machine: JAVA_TOOL_OPTIONS to -Dfile.encoding=UTF-8 Then when start gradle task info Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8 Gradle ╨▓╨ВтАЭ ╨б╨Г╨а╤С╨б╨Г╨бтАЪ╨а┬╡╨а╤Ш╨а┬░ ╨а┬░╨а╨Ж╨бтАЪ╨а╤Х╨а╤Ш╨а┬░╨бтАЪ╨а╤С╨бтАб╨а┬╡╨б╨Г╨а╤Ф╨а╤Х╨атДЦ ╨б╨Г╨а┬▒╨а╤Х╨б╨В╨а╤Ф╨а╤С, Where I do something wrong?Archfiend
I don't know what you mean by "Gradle task info picked up JAVA_TOOL_OPTIONS". Try to set GRADLE_OPTS (on Windows you may need to open a new command prompt after that), make sure to stop the daemon once (gradle --stop), and edit the file to force its recompilation. If that doesn't help, chances are that your file isn't utf-8.Maunsell
Adding systemProp.file.encoding=utf-8 to gradle.properties doesn't work. GRADLE_OPTS works.Caesium
Shouldn't be it sysProp.file.encoding=utf-8 rather than systemProp...? docs.gradle.org/current/userguide/…Longinus
I'm having similar problems, and been trying to resolve with the suggestions given here (running on Windows). I've verified that my file.encoding is set to utf-8 ('println System.getProperty("file.encoding")), but still can't output special characters correctly. Any suggestions? Thank you.Likeminded
@rwalker, are you sure that both steps reside within the same Jenkins node? (one being the code part where you write file.encoding to console and the other being the code part where your special character outputting actually fails)Horne
@tapa-save answer solved the problem from the project settings which is best for meJoly
V
101

I use next setting in build.gradle and it works fine for me:

compileJava.options.encoding = 'UTF-8'

tasks.withType(JavaCompile) {
    options.encoding = 'UTF-8'
}
Vincennes answered 11/1, 2016 at 8:40 Comment(3)
I believe that iterating over all JavaCompile task will also set the encoding the compileJava task. Thus, you don't need the first line in your snippet.Vespers
not working for me, I think here is not the last one for encoding set, this has been overwritten later I thinkBetel
Intellij settings, compiler options didn't work for me, this did.Affix
M
40

The file.encoding system property needs to be set right when the JVM executing the Gradle build (e.g. the Gradle Daemon) starts up. One way to achieve this is with export GRADLE_OPTS="-Dfile.encoding=utf-8". Another way that might work is to add systemProp.file.encoding=utf-8 to gradle.properties. Of course this assumes that the build script files are actually using utf-8 encoding. To see what your platform's (and therefore Gradle's) default encoding is, print out the system property's value in a build script.

Maunsell answered 21/1, 2014 at 19:47 Comment(9)
My System file.encoding=Cp1251. So if I change it on my machine to UTF-8 will this solve my problem in general?Archfiend
What matters is the Java platform encoding.Maunsell
OK .I add System Environment Variable on my machine: JAVA_TOOL_OPTIONS to -Dfile.encoding=UTF-8 Then when start gradle task info Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8 Gradle ╨▓╨ВтАЭ ╨б╨Г╨а╤С╨б╨Г╨бтАЪ╨а┬╡╨а╤Ш╨а┬░ ╨а┬░╨а╨Ж╨бтАЪ╨а╤Х╨а╤Ш╨а┬░╨бтАЪ╨а╤С╨бтАб╨а┬╡╨б╨Г╨а╤Ф╨а╤Х╨атДЦ ╨б╨Г╨а┬▒╨а╤Х╨б╨В╨а╤Ф╨а╤С, Where I do something wrong?Archfiend
I don't know what you mean by "Gradle task info picked up JAVA_TOOL_OPTIONS". Try to set GRADLE_OPTS (on Windows you may need to open a new command prompt after that), make sure to stop the daemon once (gradle --stop), and edit the file to force its recompilation. If that doesn't help, chances are that your file isn't utf-8.Maunsell
Adding systemProp.file.encoding=utf-8 to gradle.properties doesn't work. GRADLE_OPTS works.Caesium
Shouldn't be it sysProp.file.encoding=utf-8 rather than systemProp...? docs.gradle.org/current/userguide/…Longinus
I'm having similar problems, and been trying to resolve with the suggestions given here (running on Windows). I've verified that my file.encoding is set to utf-8 ('println System.getProperty("file.encoding")), but still can't output special characters correctly. Any suggestions? Thank you.Likeminded
@rwalker, are you sure that both steps reside within the same Jenkins node? (one being the code part where you write file.encoding to console and the other being the code part where your special character outputting actually fails)Horne
@tapa-save answer solved the problem from the project settings which is best for meJoly
C
36

In gradle.properties

org.gradle.jvmargs='-Dfile.encoding=UTF-8'
Cheslie answered 25/6, 2017 at 3:12 Comment(2)
Thank you, burned an hour on this. This was the only thing that worked for running JUnit tests in a Gradle project in IDEA. Even "systemProp.file.encoding=utf-8" in gradle.properties didn't do it oddly enough.Rasmussen
It seems that gradle.properties is not generated, at least since Gradle 7.6.Follansbee
S
15

if you're using kotlin DSL, then:

build.gradle.kts:

tasks.withType<JavaCompile> {
    options.encoding = "UTF-8"
}
Sulphide answered 29/10, 2019 at 22:14 Comment(0)
C
9

For my only works with this in build.gradle:

apply plugin: 'java'
tasks.withType(JavaCompile) {
    options.encoding = 'UTF-8'
}
repositories {
    mavenCentral()
}
....
test {
    systemProperty "file.encoding", "utf-8"
}
Childhood answered 29/4, 2016 at 8:8 Comment(0)
W
1

Possible solution is to wrap gradle build call into the sh script and export encoding settings in this script: buildScript.sh:

export GRADLE_OPTS="-Dfile.encoding=utf-8" 
gradlew clean build ...

P.S. this solution has been successfully checked and provided

Whittling answered 11/12, 2021 at 21:57 Comment(1)
Don't wrap the Gradle build. Use the gradle.properties to configure the file encoding. Done. https://mcmap.net/q/336865/-show-utf-8-text-properly-in-gradleDecorum
G
1

In my case, I wrote Chinese in ~/.gradle/gradle.properties, and then I added systemProp.file.encoding=utf-8 and export GRADLE_OPTS="-Dfile.encoding=utf -8" does not take effect.

I think the reason may be that before loading these things, ~/.gradle/gradle.properties has been loaded in the default encoding.

My final solution is to convert Chinese to Unicode, such as \u6211\u7684\u8d44\u6e90 and then write ~/.gradle/gradle.properties.

Grobe answered 20/12, 2023 at 13:18 Comment(0)
S
0

All solution above need changes to IDE settings and IDE restart.

I managed to solve this problem by adding some lines to build.gradle

compileJava.options.encoding = 'UTF-8'
tasks.withType(JavaCompile) {
    options.encoding = 'UTF-8'
}

test {
    systemProperty "file.encoding", "utf-8"
    useJUnitPlatform()
}
Shunt answered 27/3, 2022 at 20:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.