当我在我的多模块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

其他回答

似乎人们混合了内容编码和构建文件/资源编码。只有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”)

希望这对一些人有用。

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

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

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

试试这个:

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

好的,我找到问题了。

我使用一些报告插件。在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