maven 编译 run test的时候控制台输出乱码信息

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

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

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

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

这可能会致使乱码问题.首先将forkMode设置为never,即不新建.再运行mvn test,所有OK了.果真是这个问题!! 单元测试

因而再找官方参数说明,发现了argLine参数,这个参数与forkMode一块儿使用,能够设置新建JVM时的JVM启动参数,测试

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

仍是每次新建一个JVM进程.再次运行mvn test,一块儿OK了,问题解决.编码

 

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

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

 

部分 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>

跳过单元测试

<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>2.5</version>
				<configuration>
				  <argLine>-Dfile.encoding=UTF-8</argLine>
				   <skip>true</skip>   
				</configuration>
			</plugin>
相关文章
相关标签/搜索