eclipse-->run as --> maven test 中文乱码

来源:互联网 发布:西安爱易网络赵奇照片 编辑:程序博客网 时间:2024/05/22 01:37

今天建了个maven project 测试 mybaits

直接运行 junit测试用例的时候,输出中文正常

用 eclipse-->run as --> maven test  就出现中文乱码

从网上找到解决的办法:

原文:http://monsoongale.iteye.com/blog/1028926

maven-surefire-plugin是运行mvn test时执行测试的插件,

其有一个配置参数forkMode,默认为once,即表示每次运行test时,新建一个JVM进程运行所有test.

这可能会导致乱码问题.首先将forkMode设置为never,即不新建.再运行mvn test,全部OK了.果然是这个问题!!

于是再找官方参数说明,发现了argLine参数,这个参数与forkMode一起使用,可以设置新建JVM时的JVM启动参数,

于是设置<argLine>-Dfile.encoding=UTF-8</argLine>,明确指定一下JVM的file.encoding,并将forkMode从never改回once,

还是每次新建一个JVM进程.再次运行mvn test,一起OK了,问题解决.

 

究其原因,是因为MAVEN的maven-surefire-plugin在新建JVM进程后,由于没有指定encoding,

采用了OS的默认编码,导致读取UTF-8文件(测试类的sql脚本文件)时出现乱码

 

部分 pom.xml 代码

<build><plugins><!-- 解决maven test命令时console出现中文乱码乱码 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>2.7.2</version><configuration><forkMode>once</forkMode> <argLine>-Dfile.encoding=UTF-8</argLine> <!-- <systemProperties> --><!-- <property> --><!-- <name>net.sourceforge.cobertura.datafile</name> --><!-- <value>target/cobertura/cobertura.ser</value> --><!-- </property> --><!-- </systemProperties> --></configuration></plugin></plugins></build>


 

3 0