2012-05-31 40 views
5

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

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 

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

+0

设置平台的默认编码为UTF-8解决了这个问题。我认为这是Maven中的一个错误。 – roesslerj

+0

你使用哪个版本的编译器插件?你有没有在你的pom中定义project.build.SourceEnconding(看起来你没有) – khmarbaise

+0

编译器插件的版本没有在pom中指定,Maven的版本是“2.2.1(rdebian-4)”。我试图在pom中指定'project.build.SourceEnconding'属性,并没有改变任何东西。这应该与在命令行中提供'-Dproject.build.sourceEncoding ='相同。 – roesslerj

回答

6

遇到同样的问题我首先遇到了这个问题。但是,因为没有答案,我还看了一下,发现这是回答一个问题,帮助我解决这个:

https://stackoverflow.com/a/10375505/332248(信贷@chrisapotek和@Jopp埃根回答这个)

5

的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>