Java Maven 项目源代码编码不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19495914/
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-08-12 17:45:27  来源:igfitidea点击:

Maven project source encoding does not work

javamavenencodingutf-8

提问by scravy

I am having a Maven-Project which is configured using the following properties:

我有一个使用以下属性配置的 Maven 项目:

<properties>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>

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

On my machine everything works fine with these settings. However, on another machine, when I check it out and try to build it using mvn installthe compiler errors unmappable character for encoding ASCII:

在我的机器上,这些设置一切正常。但是,在另一台机器上,当我检查它并尝试使用mvn install编译器错误构建它时unmappable character for encoding ASCII

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.0.2:compile (default-compile) on project scuttle: Compilation failure: Compilation failure:
[ERROR] /export/local-1/julian-downloads/scuttle/src/main/java/de/fu/mi/scuttle/handlers/sakai/SakaiVV.java:[99,39] error: unmappable character for encoding ASCII
[ERROR] 
[ERROR] /export/local-1/julian-downloads/scuttle/src/main/java/de/fu/mi/scuttle/handlers/sakai/SakaiVV.java:[99,40] error: unmappable character for encoding ASCII
[ERROR] 
[ERROR] /export/local-1/julian-downloads/scuttle/src/main/java/de/fu/mi/scuttle/domain/sakai/SakvvTermin.java:[66,30] error: unmappable character for encoding ASCII
[ERROR] 
[ERROR] /export/local-1/julian-downloads/scuttle/src/main/java/de/fu/mi/scuttle/domain/sakai/SakvvTermin.java:[66,31] error: unmappable character for encoding ASCII

I've already tried running mvnwith -Dfile.encoding=UTF-8but that did not help. $LC_CTYPEreports UTF-8.

我已经尝试过跑步mvn-Dfile.encoding=UTF-8但这没有帮助。$LC_CTYPE报道UTF-8

What can I do?

我能做什么?

You can see the complete pom file here: https://github.com/scravy/scuttle/blob/master/pom.xml

你可以在这里看到完整的 pom 文件:https: //github.com/scravy/scuttle/blob/master/pom.xml

采纳答案by scravy

In the end the comment by khmarbaise helped me find a solution:

最后 khmarbaise 的评论帮助我找到了解决方案:

I configured the compiler plugin and specified the source encoding there, now everything works everywhere:

我配置了编译器插件并在那里指定了源编码,现在一切正常:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>2.3.2</version>
      <configuration>
        <source>1.7</source>
        <target>1.7</target>
        <encoding>UTF-8</encoding>
      </configuration>
    </plugin>
  </plugins>
</build>

回答by Vidya

You have all the right settings in your pom, so that's good. Verify your IDE (and the others used by your team if applicable) is configured for UTF-8.

你的 pom 中有所有正确的设置,所以很好。验证您的 IDE(以及您的团队使用的其他 IDE,如果适用)已配置为 UTF-8。

Next, clean out your local .m2repository and run your build again. It might be possible one of your transitive dependencies wasn't compiled with the right encoding. But you may have the "right" version in your .m2that makes everything OK while the same version doesn't exist on the other machine.

接下来,清理您的本地.m2存储库并再次运行您的构建。您的传递依赖项之一可能没有使用正确的编码进行编译。但是您可能有“正确”的版本,.m2这使得一切正常,而另一台机器上不存在相同的版本。

Good luck! I know how annoying this stuff can be.

祝你好运!我知道这些东西有多烦人。

回答by BSeitkazin

Instead of setting mavenopts, set new environmental variable - JAVA_TOOL_OPTIONS = -Dfile.encoding=UTF8

而不是设置mavenopts,设置新的环境变量 -JAVA_TOOL_OPTIONS = -Dfile.encoding=UTF8