简体   繁体   English

从`pom.xml`设置`defaultCharset`

[英]Set `defaultCharset` from `pom.xml`

This is a follow up to set file.encoding from pom.xml . 这是pom.xml设置file.encoding的后续步骤。 I need to make sure that Charset.defaultCharset() returns UTF-8 . 我需要确保Charset.defaultCharset()返回UTF-8 When I run 当我跑步

JAVA_TOOL_OPTIONS='-Dfile.encoding=UTF-8' mvn clean compile test package

I get that. 我明白了。 However, I do not have any control about how mvn is invoked, so I need to put something into pom.xml which would set the default charset to utf-8 . 但是,我对如何调用mvn没有任何控制,因此我需要在pom.xml中放入一些东西,这会将默认字符集设置为utf-8 Which systemPropertyVariables do I set? 我应该设置哪个systemPropertyVariables

EDIT: I do have 编辑:我确实有

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

and

<configuration>
  <systemPropertyVariables>
    <file.encoding>${project.build.sourceEncoding}</file.encoding>
    <charset>${project.build.sourceEncoding}</charset>
  </systemPropertyVariables>
</configuration>

they do not help. 他们没有帮助。 defaultCharset is still US-ASCII . defaultCharset仍然是US-ASCII

You can set the following property in the pom: 您可以在pom中设置以下属性:

<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

maven-compiler-plugin uses this property to set the Java compiler -encoding option. maven-compiler-plugin使用此属性来设置Java编译器-encoding选项。

See: http://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html#encoding 请参阅: http : //maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html#encoding

You can try including that setting in the MAVEN_OPTS environment variable, examples in Maven Docs and stackoverflow . 您可以尝试将该设置包含在MAVEN_OPTS环境变量中, Maven Docs中的示例和stackoverflow中

Or, if you don't want to configure a global environment var, the Codehaus properties-maven-plugin might work too, depending on your use case. 或者,如果您不想配置全局环境var,则根据您的使用情况,Codehaus properties-maven-plugin可能也可以工作。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM