Java 如何在Maven中配置编码?

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

How to configure encoding in Maven?

javamavenencodingmaven-2maven-3

提问by Ethan Leroy

When I run maven installon my multi module maven project I always get the following output:

当我maven install在我的多模块 maven 项目上运行时,我总是得到以下输出:

[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!

So, I googled around a bit, but all I can find is that I have to add:

所以,我用谷歌搜索了一下,但我能找到的只是我必须添加:

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

...to my pom.xml. But it's already there (in the parent pom.xml).

...到我的 pom.xml。但它已经存在(在 parent 中pom.xml)。

Configuring <encoding>for the maven-resources-plugin or the maven-compiler-plugin also doesn't fix it.

配置<encoding>maven-resources-plugin 或 maven-compiler-plugin 也不能修复它。

So what's the problem?

所以有什么问题?

采纳答案by Ethan Leroy

OK, I found the problem.

好的,我发现了问题。

I use some reporting plugins. In the documentation of the failsafe-maven-plugin (http://maven.apache.org/plugins/maven-failsafe-plugin/integration-test-mojo.html) I found, that the <encoding>configuration - of course - uses ${project.reporting.outputEncoding}by default. So I added the property as a child element of the projectelement and everything is fine now:

我使用了一些报告插件。在failsafe-maven-plugin(http://maven.apache.org/plugins/maven-failsafe-plugin/integration-test-mojo.html)的文档中,我发现,<encoding>配置——当然——${project.reporting.outputEncoding}默认使用. 所以我添加了属性作为元素的子元素,project现在一切都很好:

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

See also http://maven.apache.org/general.html#encoding-warning

也可以看看 http://maven.apache.org/general.html#encoding-warning

回答by Ville Myrskyneva

This would be in addition to previous, if someone meets a problem with scandic letters that isn't solved with the solution above.

如果有人遇到上述解决方案无法解决的扫描字母问题,这将是对先前的补充。

If the java source files contain scandic letters they need to be interpreted correctly by the Java used for compiling. (e.g. scandic letters used in constants)

如果 java 源文件包含 scandic 字母,它们需要被用于编译Java正确解释。(例如,常量中使用的扫描字母)

Even that the files are stored in UTF-8 and the Maven is configured to use UTF-8, the System Java used by the Maven will still use the system default (eg. in Windows: cp1252).

即使文件以 UTF-8 存储并且 Maven 配置为使用 UTF-8,Maven 使用的系统 Java 仍将使用系统默认值(例如,在 Windows 中:cp1252)。

This will be visible only running the tests via maven (possibly printing the values of these constants in tests. The printed scandic letters would show as '< ?>') If not tested properly, this would corrupt the class files as compile result and be left unnoticed.

这仅在通过 maven 运行测试时才可见(可能在测试中打印这些常量的值。打印的扫描字母将显示为“< ?>”)如果没有正确测试,这将破坏类文件作为编译结果并被忽视了。

To prevent this, you have to set the Java used for compilingto use UTF-8 encoding. It is not enough to have the encoding settings in the maven pom.xml, you need to set the environment variable: JAVA_TOOL_OPTIONS = -Dfile.encoding=UTF8

为了防止这种情况,您必须将用于编译Java设置为使用 UTF-8 编码。maven pom.xml 里有编码设置是不够的,需要设置环境变量:JAVA_TOOL_OPTIONS = -Dfile.encoding=UTF8

Also, if using Eclipse in Windows, you may need to set the encoding used in addition to this (if you run individual test via eclipse).

此外,如果在 Windows 中使用 Eclipse,您可能需要设置除此之外使用的编码(如果您通过 Eclipse 运行单独的测试)。

回答by fsimon

Try this:

尝试这个:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.7</version>
        <configuration>
          ...
          <encoding>UTF-8</encoding>
          ...
        </configuration>
      </plugin>
    </plugins>
    ...
  </build>
  ...
</project>

回答by bhdrk

If you combine the answers above, finally a pom.xml that configured for UTF-8 should seem like that.

如果你结合上面的答案,最后一个为 UTF-8 配置的 pom.xml 应该看起来像这样。

pom.xml

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>YOUR_COMPANY</groupId>
    <artifactId>YOUR_APP</artifactId>
    <version>1.0.0-SNAPSHOT</version>

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

    <dependencies>
        <!-- Your dependencies -->
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>${project.java.version}</source>
                    <target>${project.java.version}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.0.2</version>
                <configuration>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

回答by isapir

In my case I was using the maven-dependency-pluginso in order to resolve the issue I had to add the following property:

在我的情况下,我使用maven-dependency-pluginso 来解决我必须添加以下属性的问题:

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

See Apache Maven Resources Plugin / Specifying a character encoding scheme

请参阅Apache Maven 资源插件/指定字符编码方案

回答by Alexandr

It seems people mix a content encoding with a built files/resources encoding. Having only maven properties is not enough. Having -Dfile.encoding=UTF8not effective. To avoid having issues with encoding you should follow the following simple rules

似乎人们将内容编码与构建的文件/资源​​编码混合在一起。只有 Maven 属性是不够的。有-Dfile.encoding=UTF8没有效果。为避免出现编码问题,您应该遵循以下简单规则

  1. Set maven encoding, as described above:
  1. 设置maven编码,如上所述:
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  1. Always set encoding explicitly, when work with files, strings, IO in your code. If you do not follow this rule, your application depend on the environment. The -Dfile.encoding=UTF8exactly is responsible for run-time environment configuration, but we should not depend on it. If you have thousands of clients, it takes more effort to configure systems and to find issues because of it. You just have an additional dependency on it which you can avoid by setting it explicitly. Most methods in Java that use a default encoding are marked as deprecated because of it.

  2. Make sure the content, you are working with, also is in the same encoding, that you expect. If it is not, the previous steps do not matter! For instance a file will not be processed correctly, if its encoding is not UTF8 but you expect it. To check file encoding on Linux:

  1. 在代码中处理文件、字符串、IO 时,始终明确设置编码。如果您不遵循此规则,您的应用程序将依赖于环境。在-Dfile.encoding=UTF8究竟是负责运行时环境的配置,但是我们不应该依赖于它。如果您有数千个客户端,则需要花费更多的精力来配置系统并因此发现问题。你只是对它有一个额外的依赖,你可以通过显式设置来避免它。Java 中使用默认编码的大多数方法都因此被标记为已弃用。

  2. 确保您使用的内容也与您期望的编码相同。如果不是,前面的步骤就无所谓了!例如,如果文件的编码不是 UTF8 但您期望它,则文件将不会被正确处理。在 Linux 上检查文件编码:

$ file --mime F_PRDAUFT.dsv

$ file --mime F_PRDAUFT.dsv

  1. Force clients/server set encoding explicitly in requests/responses, here are examples:
  1. 强制客户端/服务器在请求/响应中明确设置编码,以下是示例:
@Produces("application/json; charset=UTF-8")
@Consumes("application/json; charset=UTF-8")
@Produces("application/json; charset=UTF-8")
@Consumes("application/json; charset=UTF-8")

Hope this will be useful to someone.

希望这对某人有用。