gpt4 book ai didi

java - MVN编译不使用UTF-8编码

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:31:56 25 4
gpt4 key购买 nike

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

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

(因此设置感知平台编码和源文件编码,参见 maven platform encoding)我得到类似的东西

[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

现在当我使用

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

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

最佳答案

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>

关于java - MVN编译不使用UTF-8编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10842635/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com