简体   繁体   English

gradle 如何决定 file.encoding JVM 系统属性?

[英]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

Update #1更新 #1

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

Update #2更新 #2

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.encodingjavac -encoding ),而不是写一吨if语句来避免发送-Dfile.encoding-encoding时没有明确的编码是设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java如何确定&#39;file.encoding&#39;系统属性? - How does Java determine 'file.encoding' system property? Java中的file.encoding系统属性在JVM的不同实例中给出不同的结果 - file.encoding system property in Java giving different results in different instances of JVM 始终在Java中设置file.encoding系统属性是一个好主意吗? - Is it a good idea to always set file.encoding system property in Java? Spring Boot WIndows 10 系统属性'file.encoding' - Spring Boot WIndows 10 System property 'file.encoding' 谁将 JVM file.encoding 重置为原始值? - Who resets JVM file.encoding back to original? E / System:忽略尝试将属性“ file.encoding”设置为值“ ISO-8859-1” - E/System: Ignoring attempt to set property “file.encoding” to value “ISO-8859-1” System.setProperty无法在Java中用于file.encoding - System.setProperty not working for file.encoding in Java 如何在ant中为junit测试设置file.encoding? - How do I set file.encoding for a junit test in ant? 如何测试具有特定 file.encoding 选项的方法? - How to test a method with specific file.encoding option? 为什么即使在设置了 file.encoding 的情况下,在属性文件中使用 Unicode 仍然有效,但实际字符却无效? - Why does using Unicode in a properties file work but not the actual character even when file.encoding is set?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM