MVN编译不使用UTF-8编码

roe*_*erj 8 java encoding maven

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

mvn -Dfile.encoding=UTF-8 -Dproject.build.sourceEncoding=UTF-8 test
Run Code Online (Sandbox Code Playgroud)

(因此设置感知平台编码和源文件编码,请参阅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
Run Code Online (Sandbox Code Playgroud)

当我用它编译文件时

javac path/to/file.java
Run Code Online (Sandbox Code Playgroud)

我得到类似的错误:

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
Run Code Online (Sandbox Code Playgroud)

现在我用的时候

javac -encoding UTF-8 path/to/file.java
Run Code Online (Sandbox Code Playgroud)

相反,我得到cannot find symbol错误,因为缺少依赖性.所以我认为问题是javac在编译测试时没有在Maven中使用UTF-8选项调用(请注意insection中Using 'UTF-8' encoding to copy filtered resources.缺少的内容compiler:testCompile).这个结论是否正确?我错过了什么吗?这是一个已知的问题吗?我能做些什么吗?显然,我系统上的平台编码不是UTF-8,但我目前无法改变它.

Sai*_*ish 12

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>
Run Code Online (Sandbox Code Playgroud)


Jen*_*ens 7

遇到同样的问题后,我首先遇到了这个问题.但由于没有答案,我进一步观察,发现另一个问题得到了回答并帮助我解决了这个问题:

/sf/answers/726285381/(感谢@chrisapotek和@Jopp Eggen回答此问题)