使用maven UTF-8编码的源文件进行编译失败

时间:2023-01-06 11:53:22

I am using maven to build a project. The source files are UTF-8 encoded. My pom.xml (relevant fragments) is like this:

我正在使用maven来构建一个项目。源文件是UTF-8编码的。我的pom.xml(相关片段)是这样的:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.source>1.8</maven.compiler.source>
    <JAVA_1_8_HOME>C:\Program Files\Java\jdk1.8.0_121</JAVA_1_8_HOME>
</properties>

...

    <plugins>

        <!-- to compile not with default JAVA_HOME but custom path -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5</version>
            <configuration>
                <verbose>true</verbose>
                <fork>true</fork>
                <executable>${JAVA_1_8_HOME}/bin/javac</executable>
                <compilerVersion>1.3</compilerVersion>
                <encoding>${project.build.sourceEncoding}</encoding>
            </configuration>
        </plugin>
...
    </plugins>

Now I have read the numerous posts/questions on the subject but none of the suggestions worked. I have tried setting encoding explicitly on maven-compiler-plugin but it still does not work. The error I get is:

现在我已经阅读了关于这个主题的众多帖子/问题,但没有一个建议有效。我已经尝试在maven-compiler-plugin上显式设置编码,但它仍然不起作用。我得到的错误是:

[ERROR] *my source file - name removed*.java:[523,182] error: unmappable character for encoding Cp1253
[ERROR] *my source file - name removed*.java:[523,189] error: unmappable character for encoding Cp1253

However maven-resources-plugin does pick up project.build.sourceEncoding:

但是maven-resources-plugin确实选择了project.build.sourceEncoding:

[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ XXXXXXXXXX ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]

The only way I could get the project to compile was to set JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF8.

我可以让项目编译的唯一方法是设置JAVA_TOOL_OPTIONS = -Dfile.encoding = UTF8。

I have tried maven v3.3.9 and 3.5.0 - no change.

我试过maven v3.3.9和3.5.0 - 没有变化。

2 个解决方案

#1


0  

You are configuring the default Maven behaviour in (see https://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html):

您正在配置默认的Maven行为(请参阅https://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html):

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        ...
        <encoding>${project.build.sourceEncoding}</encoding>
    </configuration>
</plugin>

You say that you set project.build.sourceEncoding to UTF-8 in your pom.xml, However the error messages say that the encoding is Cp1253. The configuration error is not in the snippet you posted. Can you post a more complete version of your pom.xml?

你说你在你的pom.xml中将project.build.sourceEncoding设置为UTF-8但是错误消息说编码是Cp1253。配置错误不在您发布的代码段中。你能发布一个更完整版的pom.xml吗?


You have to set the value UTF-8 to either the project.build.sourceEncoding property or the encoding element in one of the following 2 ways:

您必须使用以下两种方法之一将值UTF-8设置为project.build.sourceEncoding属性或encoding元素:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        ...
        <encoding>UTF-8</encoding>
    </configuration>
</plugin>

or

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

#2


-2  

Hiii,, i did have a problem like that once; add this to your pom.xml file and see if it work:

Hiii,我确实遇到过这样的问题;将其添加到您的pom.xml文件中,看看它是否有效:

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

Or there is an other way is to add envirnoment variable called JAVA_TOOL_OPTIONS, set this variable to: -Dfile.encoding=UTF8

或者还有一种方法是添加名为JAVA_TOOL_OPTIONS的envirnoment变量,将此变量设置为:-Dfile.encoding = UTF8

more about JAVA_TOOL_OPTIONS: docs.oracle

有关JAVA_TOOL_OPTIONS的更多信息:docs.oracle

#1


0  

You are configuring the default Maven behaviour in (see https://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html):

您正在配置默认的Maven行为(请参阅https://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html):

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        ...
        <encoding>${project.build.sourceEncoding}</encoding>
    </configuration>
</plugin>

You say that you set project.build.sourceEncoding to UTF-8 in your pom.xml, However the error messages say that the encoding is Cp1253. The configuration error is not in the snippet you posted. Can you post a more complete version of your pom.xml?

你说你在你的pom.xml中将project.build.sourceEncoding设置为UTF-8但是错误消息说编码是Cp1253。配置错误不在您发布的代码段中。你能发布一个更完整版的pom.xml吗?


You have to set the value UTF-8 to either the project.build.sourceEncoding property or the encoding element in one of the following 2 ways:

您必须使用以下两种方法之一将值UTF-8设置为project.build.sourceEncoding属性或encoding元素:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        ...
        <encoding>UTF-8</encoding>
    </configuration>
</plugin>

or

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

#2


-2  

Hiii,, i did have a problem like that once; add this to your pom.xml file and see if it work:

Hiii,我确实遇到过这样的问题;将其添加到您的pom.xml文件中,看看它是否有效:

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

Or there is an other way is to add envirnoment variable called JAVA_TOOL_OPTIONS, set this variable to: -Dfile.encoding=UTF8

或者还有一种方法是添加名为JAVA_TOOL_OPTIONS的envirnoment变量,将此变量设置为:-Dfile.encoding = UTF8

more about JAVA_TOOL_OPTIONS: docs.oracle

有关JAVA_TOOL_OPTIONS的更多信息:docs.oracle