如何在Maven中配置编码?

在Maven项目中遇到编码警告时,添加到pom.xml的编码配置可能无效。问题可能源于maven-resources-plugin或maven-compiler-plugin的配置,或者特定于某些报告插件。解决方案包括设置JAVA_TOOL_OPTIONS环境变量为-Dfile.encoding=UTF8,以及确保在插件配置中明确指定UTF-8。
摘要由CSDN通过智能技术生成

本文翻译自:How to configure encoding in Maven?

When I run maven install on my multi module maven project I always get the following output: 当我在我的多模块maven项目上运行maven install ,我总是得到以下输出:

[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: 所以,我google了一下,但我能找到的是我必须添加:

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

...to my pom.xml. ...到我的pom.xml。 But it's already there (in the parent pom.xml ). 但它已经存在(在父pom.xml )。

Configuring <encoding> for the maven-resources-plugin or the maven-compiler-plugin also doesn't fix it. 为maven-resources-plugin或maven-compiler-plugin配置<encoding>也无法修复它。

So what's the problem? 所以有什么问题?


#1楼

参考:https://stackoom.com/question/Cf2V/如何在Maven中配置编码


#2楼

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 . 如果java源文件包含scandic字母,则需要由用于编译Java正确解释它们。 (eg scandic letters used in constants) (例如常量中使用的scandic字母)

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运行测试才能看到这一点(可能在测试中打印这些常量的值。打印的scandic字母显示为'<?>')如果没有正确测试,这会破坏类文件作为编译结果并且是没有注意到。

To prevent this, you have to set the Java used for compiling to use UTF-8 encoding. 要防止这种情况,您必须将用于编译Java设置为使用UTF-8编码。 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 仅在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运行单独测试)。


#3楼

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. 在failsafe-maven-plugin( http://maven.apache.org/plugins/maven-failsafe-plugin/integration-test-mojo.html )的文档中,我发现<encoding>配置 - 当然 -默认情况下使用${project.reporting.outputEncoding} So I added the property as a child element of the project element and everything is fine now: 所以我将属性添加为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


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

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>

#6楼

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-dependency-plugin所以为了解决这个问题,我必须添加以下属性:

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

See Apache Maven Resources Plugin / Specifying a character encoding scheme 请参阅Apache Maven Resources插件/指定字符编码方案

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值