`

maven maven-surefire-plugin的乱码问题<转>

阅读更多

 

maven maven-surefire-plugin的乱码问题

 

今天项目中出现奇怪问题,在eclipse中直接运行TestNG时,全部都OK,但是执行mvn test时却失败.观察其输出日志,发现有乱码,怀疑是乱码导致.

最终在官网发现蛛丝马迹.

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脚本文件)时出现乱码,最终影响了TestNG的正确执行.

 

 

 

Maven Test Coverage : Cobertura and Surefire working

 

I wanted to add Cobertura to Grizzly project within the pom file for Maven, but I got errors at first.

I follow the procedure describe on Cobertura website, but it was lacking of a complete example. Cobertura will instrument your classes and you need to run tests to get a test coverage. You need to pass the path of these instrumented classes to the plugin that will run the tests(in the same pattern for ant and junit).

For my project I'm using Surefire.

I created a file pom2.xml for Grizzly. Put this file in the root of the project and call it like that : mvn -f pom2.xml cobertura:cobertura

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <parent>
        <groupId>com.sun.grizzly</groupId>
        <artifactId>grizzly-project</artifactId>
        <version>1.9.0-SNAPSHOT</version>
        <relativePath>pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.sun.grizzly</groupId>
    <artifactId>grizzly-framework</artifactId>
    <version>${grizzly-version}</version>
    <name>grizzly-framework</name>
    <url>https://grizzly.dev.java.net</url>
    <build>
        <plugins>
            <plugin>
                            <groupId>org.codehaus.mojo</groupId>
                            <artifactId>cobertura-maven-plugin</artifactId>
                            <executions>
                                <execution>
                                    <goals>
                                        <goal>clean</goal>
                                    </goals>
                                </execution>
                            </executions>
                        </plugin>
                        <plugin>
                            <groupId>org.apache.maven.plugins</groupId>
                            <artifactId>maven-surefire-plugin</artifactId>
                            <configuration>
                                <systemProperties>
                                    <property>
                                    <name>net.sourceforge.cobertura.datafile</name>
                                    <value>target/cobertura/cobertura.ser</value>
                                    </property>
                                </systemProperties>
                            </configuration>
                        </plugin>
        </plugins>
    </build>
    <reporting>
        <plugins>
            <plugin>
                            <groupId>org.codehaus.mojo</groupId>
                            <artifactId>cobertura-maven-plugin</artifactId>
                            <version>2.1</version>
                        </plugin>
                            <plugin>
                                <groupId>org.apache.maven.plugins</groupId>
                                <artifactId>maven-surefire-report-plugin</artifactId>
                                <version>2.0</version>
                            </plugin>
                            <plugin>
                                <groupId>org.apache.maven.plugins</groupId>
                                <artifactId>maven-surefire-plugin</artifactId>
                                <version>2.2</version>
                            </plugin>
        </plugins>
    </reporting>
</project>

I put explicit version of the plugins. You can remove that to use the latest version.

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/gongxinchang/archive/2009/12/29/5094712.aspx

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics