[英]How does gradle decide the file.encoding JVM system property?
When compiling, running tests, or running a main class, gradle launches java with an explicit file.encoding
, for example:在编译、运行测试或运行主类时,gradle 使用显式
file.encoding
启动 java,例如:
/usr/bin/java -Dfile.encoding=ISO-8859-1 -Duser.country=GB -Duser.language=en
-Duser.variant -cp … com.foo.MyMainClass
How does gradle select this default file encoding (which is ISO-8859-1 for me)? gradle 如何选择这个默认文件编码(对我来说是 ISO-8859-1)? To switch to UTF-8, I have to add the following to my gradle build script:
要切换到 UTF-8,我必须将以下内容添加到我的 gradle 构建脚本中:
allprojects {
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
tasks.withType(Test) {
systemProperty 'file.encoding', 'UTF-8'
}
tasks.withType(JavaExec) {
systemProperty 'file.encoding', 'UTF-8'
}
}
which sets -Dfile.encoding=UTF-8
.其中设置
-Dfile.encoding=UTF-8
。
rzwitserloot noted that gradle is likely using the default encoding, which appears to be true. rzwitserloot指出 gradle 很可能使用默认编码,这似乎是正确的。
PrintCharset.java打印字符集
import java.nio.charset.Charset;
public class PrintCharset {
public static void main(String[] args) {
System.out.println("Default: " + Charset.defaultCharset());
}
}
Then, javac PrintCharset.java && java PrintCharset
prints然后,
javac PrintCharset.java && java PrintCharset
打印
Default: ISO-8859-1
Interestingly, running from IntelliJ (without gradle) executes:有趣的是,从 IntelliJ(没有 gradle)运行会执行:
/usr/bin/java -javaagent:… -Dfile.encoding=UTF-8 -classpath … PrintCharset
which, of course, prints Default: UTF-8
.当然,它会打印
Default: UTF-8
。
Digging a little deeper out of curiosity, the default charset in OpenJDK on linux is ultimately determined by nl_langinfo()
:出于好奇深入挖掘,Linux 上 OpenJDK 中的默认字符集最终由
nl_langinfo()
确定:
printlang.c打印语言
#include <langinfo.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
setlocale(LC_ALL, "");
printf("%s\n", nl_langinfo(CODESET));
exit(EXIT_SUCCESS);
}
Then, gcc -o printlang printlang.c && ./printlang
prints ISO-8859-1
.然后,
gcc -o printlang printlang.c && ./printlang
打印ISO-8859-1
。
TL;DR: Who knows, but probably Charset.defaultEncoding. TL;DR:谁知道,但可能是 Charset.defaultEncoding。
With some more information:提供更多信息:
Presumably, it's your platform default.据推测,这是您的平台默认设置。 There's a long chain that gradle goes through.
gradle 有一条长链。 First there's the explicit override on the task itself, then there's the ability to override this value for all such tasks (which is what your snippet of gradle does), then there's the
GRADLE_OPTS=-Dfile.encoding=....
option which gradle would look at if the task itself doesn't explicitly pick one.首先是任务本身的显式覆盖,然后有能力为所有此类任务覆盖此值(这是您的 gradle 片段所做的),然后是
GRADLE_OPTS=-Dfile.encoding=....
选项 which gradle会查看任务本身是否没有明确选择一个。 The docs more or less end there, but presumably it'll then fall back to whatever Charset.defaultEncoding()
returns (you can write a 1-liner java app and print that, have a look), and it's simpler for gradle to just retrieve the name of this charset and then apply it everywhere it sets encoding (be it java -Dfile.encoding
or javac -encoding
) instead of writing a ton of if
statements to avoid sending -Dfile.encoding
or -encoding
when no explicit encoding was set at all.文档或多或少在那里结束,但大概它会回
Charset.defaultEncoding()
返回的任何Charset.defaultEncoding()
您可以编写一个 1-liner java 应用程序并打印它,看看),并且对于 gradle 来说更简单检索该字符集的名称,然后应用它无处不在,它集编码(无论是java -Dfile.encoding
或javac -encoding
),而不是写一吨if
语句来避免发送-Dfile.encoding
或-encoding
时没有明确的编码是设置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.