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

 

Maven插件是一种可插拔的工具,可以在Maven构建过程中执行特定的任务。其中,maven-dependency-plugin和maven-surefire-plugin是两个常用的插件。 maven-dependency-plugin插件可以用来管理项目依赖,可以帮助我们列出项目中的依赖关系,复制依赖文件到指定目录,解压依赖文件等。常用的配置包括: - list:列出项目依赖 - copy-dependencies:将所有依赖文件复制到指定目录 - unpack:解压指定的依赖文件 maven-surefire-plugin插件则是用来执行项目的单元测试的。它可以在Maven构建过程中自动执行单元测试,并生成测试报告。常用的配置包括: - includes/excludes:指定要执行的测试类或排除的测试类 - parallel:指定测试是否并行执行 - reportsDirectory:指定测试报告生成的目录 在POM文件中配置这两个插件,可以通过以下方式: ``` <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>3.2.0</version> <executions> <execution> <id>copy-dependencies</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/lib</outputDirectory> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M5</version> <configuration> <includes> <include>**/*Test.java</include> </includes> <parallel>methods</parallel> <threadCount>10</threadCount> <reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory> </configuration> </plugin> </plugins> </build> ``` 以上是一个简单的POM文件中Maven插件配置maven-dependency-plugin和maven-surefire-plugin的示例,其中maven-dependency-plugin在package阶段执行复制依赖文件的任务,maven-surefire-plugintest阶段执行单元测试。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值