java 有没有办法在不使用外部 JAVA_TOOL_OPTIONS 的情况下使用 UTF-8 使 Maven 构建类文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10368527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 00:43:22  来源:igfitidea点击:

Is there a way to make maven build class files with UTF-8 without using the external JAVA_TOOL_OPTIONS?

javamavenmaven-2character-encoding

提问by chrisapotek

I don't want to be dependable on a external environment variable to force maven to build my classes with UTF-8. On Mac, I was getting all sorts of problems when building with maven. Only the option below solved the problem:

我不想依赖外部环境变量来强制 maven 使用 UTF-8 构建我的类。在 Mac 上,我在使用 maven 构建时遇到了各种各样的问题。只有以下选项解决了问题:

export JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF-8
mvn clean install

However I am distributing my project and it does NOT make sense to rely on the user to set this environment variable to build the project correctly.

但是,我正在分发我的项目,依靠用户设置此环境变量来正确构建项目是没有意义的。

Tried everything as described here: enabling UTF-8 encoding for clojure source files

按照此处所述尝试了所有内容:为 clojure 源文件启用 UTF-8 编码

Anyone has a light on that awesome Maven issue?

任何人都对那个很棒的 Maven 问题有所了解?

回答by chrisapotek

@Joop Eggen gave the right answer here: https://stackoverflow.com/a/10367745/962872

@Joop Eggen 在这里给出了正确答案:https://stackoverflow.com/a/10367745/962872

It is not enough to define that property. You MUST pass it inside the appropriate plugins. It won't go by magic inside there.

仅定义该属性是不够的。您必须在适当的插件中传递它。它不会在里面变魔术。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>...</version>
    <configuration>
        <source>1.6</source>
        <target>1.6</target>
        <encoding>${project.build.sourceEncoding}</encoding>
    </configuration>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>...</version>
    <configuration>
        <encoding>${project.build.sourceEncoding}</encoding>
    </configuration>
</plugin>
...
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

回答by Alexander Pogrebnyak

Yes there is, define

是的,定义

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

回答by dave_nedde

I was running into this problem, but only when running the compile from Emacs. I could not change the project's poms. What worked for me was to put the following in ~/.mavenrc

我遇到了这个问题,但仅限于从 Emacs 运行编译时。我无法更改项目的 poms。对我有用的是将以下内容放入 ~/.mavenrc

LANG=en_US.UTF-8