MVN编译未使用UTF-8编码

9

好的,这是一个奇怪的问题: 我有一个使用一些UTF-8字符的Java测试文件。当我使用Maven编译它时,

mvn -Dfile.encoding=UTF-8 -Dproject.build.sourceEncoding=UTF-8 test

因此,设置感知平台编码和源文件编码(参见Maven平台编码),我得到类似于以下内容:

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building project
[INFO]    task-segment: [test]
[INFO] ------------------------------------------------------------------------
[INFO] [resources:resources {execution: default-resources}]
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory path/src/main/resources
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Nothing to compile - all classes are up to date
[INFO] [resources:testResources {execution: default-testResources}]
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory path/src/test/resources
[INFO] [compiler:testCompile {execution: default-testCompile}]
[INFO] Compiling 7 source files to path/target/test-classes
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Compilation failure
path/to/file.java:[42,23] unclosed character literal
path/to/file.java:[42,25] ';' expected
path/to/file.java:[42,26] unclosed character literal
path/to/file.java:[47,23] unclosed character literal
path/to/file.java:[47,25] illegal character: \182
path/to/file.java:[47,26] unclosed character literal

当我使用编译文件时

javac path/to/file.java

我遇到了类似的错误:

path/to/file.java:42: unclosed character literal
    illegalCharEnc('ä');
                   ^
path/to/file.java:42: ';' expected
    illegalCharEnc('ä');
                     ^
path/to/file.java:42: unclosed character literal
    illegalCharEnc('ä');
                      ^
path/to/file.java:47: unclosed character literal
    illegalCharDec('ö');
                   ^
path/to/file.java:47: illegal character: \182
    illegalCharDec('ö');
                     ^
path/to/file.java:47: unclosed character literal
    illegalCharDec('ö');
                      ^
6 errors

Now when I use

javac -encoding UTF-8 path/to/file.java

相反,我收到了“找不到符号”错误,因为缺少依赖项。所以我认为问题在于,在Maven编译测试时,没有使用UTF-8选项调用javac(请注意,compiler:testCompile部分中缺少“使用'UTF-8'编码复制过滤资源。”)。这个结论正确吗?我错过了什么吗?这是一个已知的问题吗?我能做些什么吗?显然,我的系统上的平台编码不是UTF-8,但我目前无法更改它。

将平台的默认编码设置为UTF-8解决了问题。我认为这是Maven中的一个错误。 - roesslerj
你使用的编译器插件版本是哪个?你在pom中定义了project.build.SourceEncoding吗?看起来你没有定义。 - khmarbaise
编译器插件的版本未在pom中指定,Maven版本为“2.2.1(rdebian-4)”。我尝试在pom中指定project.build.SourceEnconding属性,但没有改变任何东西。而且这应该与在命令行中给出-Dproject.build.sourceEncoding=是一样的。 - roesslerj
2
那么你应该尝试在pluginManagement中定义maven-compiler-plugin版本(版本:2.5),并重新检查...因为在maven-compiler-plugin版本2.1中已经修复了编码问题(http://jira.codehaus.org/secure/ReleaseNote.jspa?projectId=11130&version=12304),这可能会影响您。但我不确定MVN 2.2.1中定义了哪个版本的maven-compiler-plugin。 - khmarbaise
@khmarbaise:如果您将此作为官方答案,我会接受它。如果您不这样做,那我会接受它 :-) - roesslerj
2个回答

22

Maven编译插件,将编码作为配置参数进行设置。不仅支持UTF-8,还支持多种编码标准。

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

不,编码的唯一全局设置是通过环境变量完成的:https://dev59.com/Hmkw5IYBdhLWcg3wUI_x#9976788 - Gangnus

7

我也遇到了同样的问题,最先看到的是这个问题。但由于没有答案,我继续查找并找到了另一个有答案并帮助我解决问题的问题:

https://dev59.com/VWkv5IYBdhLWcg3wwjgI#10375505(感谢 @chrisapotek 和 @Jopp Eggen 提供的答案)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接