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

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

所以,我在谷歌上搜索了一下,但我只能找到我必须添加的内容:

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

...到我的pom.xml。但是它已经存在(在父文件pom.xml中)。

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

那么问题是什么呢?


好的,我找到问题了。

我使用一些报告插件。在failsafe-maven-plugin的文档中,我发现<encoding>配置当然使用${project.reporting。默认为outputEncoding}。

所以我添加了属性作为项目元素的子元素,现在一切都好了:

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

参见http://maven.apache.org/general.html#encoding-warning

这将是除了前面,如果有人遇到了一个问题与可疑的字母,不能解决上述解决方案。

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

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

只有通过maven运行测试时才可见(可能会在测试中打印这些常量的值)。打印的可疑字母将显示为'< ?>') 如果没有正确地测试,这将破坏类文件作为编译结果,并且不被注意。

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

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

试试这个:

<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>

如果结合上面的答案,最后为UTF-8配置的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>

在我的情况下,我正在使用maven-dependency-plugin,所以为了解决这个问题,我必须添加以下属性:

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

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

似乎人们混合了内容编码和构建文件/资源编码。只有maven属性是不够的。-Dfile。encoding=UTF8无效。为了避免出现编码问题,您应该遵循以下简单的规则

设置maven编码,如上所述:

utf - 8 < project.build.sourceEncoding > < / project.build.sourceEncoding > utf - 8 < project.reporting.outputEncoding > < / project.reporting.outputEncoding >

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=UTF8 exactly 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. 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:

$ file——mime F_PRDAUFT.dsv

强制客户端/服务器在请求/响应中显式设置编码,以下是示例:

@Produces(“应用程序/ json;charset = UTF-8”) @Consumes(“应用程序/ json;charset = UTF-8”)

希望这对一些人有用。