如何在Maven中配置编码?

时间:2020-12-08 23:56:27

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

当我在我的多模块maven项目上运行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。但是它已经在那里了(在父form .xml中)。

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

为maven-resources-plugin或maven-compiler- compiler-plugin配置 <编码> 也不能解决这个问题。

So what's the problem?

所以有什么问题?

5 个解决方案

#1


446  

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 project element and everything is fine now:

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

<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

#2


33  

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

如果有人遇到与上述解决方案没有解决的scandic字母问题,那么这将是对前面的解决方案的补充。

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 compiling to 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中设置编码设置是不够的。需要设置环境变量: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运行单个测试)。

#3


21  

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

如果你把上面的答案组合在一起,最后是一个pom。为UTF-8配置的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>

#4


4  

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>

#5


0  

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

在我的案例中,我使用了maven- dependent -plugin,所以为了解决这个问题,我必须添加以下属性:

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

See Apache Maven Resources Plugin / Specifying a character encoding scheme

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

#1


446  

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 project element and everything is fine now:

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

<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

#2


33  

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

如果有人遇到与上述解决方案没有解决的scandic字母问题,那么这将是对前面的解决方案的补充。

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 compiling to 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中设置编码设置是不够的。需要设置环境变量: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运行单个测试)。

#3


21  

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

如果你把上面的答案组合在一起,最后是一个pom。为UTF-8配置的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>

#4


4  

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>

#5


0  

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

在我的案例中,我使用了maven- dependent -plugin,所以为了解决这个问题,我必须添加以下属性:

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

See Apache Maven Resources Plugin / Specifying a character encoding scheme

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