Maven打包时过滤测试代码或指定特定的测试类(maven-surefire-plugin)

转自:http://www.cnblogs.com/EasonJim/p/6844969.html

1、过滤整个测试代码,可以直接在命令行上指定

mvn clean install -Dmaven.test.skip=true

提示:以上为举例,具体的构建阶段可以自定义,其中maven.test.skip为是否进行测试。

或者

mvn clean install -DskipTests

还可以直接在pom.xml文件上指定,比如使用maven-surefire-plugin时的配置

复制代码
<plugin>  
    <groupId>org.apache.maven.pluginsgroupId>  
    <artifactId>maven-surefire-pluginartifactId>  
    <version>2.20version>  
    <configuration>  
        <skipTests>trueskipTests>  
    configuration>  
plugin>  
复制代码

提示:skipTests当为true为测试,反之同理。如果是使用插件,那么要把依赖的jar包去除。

通过节点配置属性

<properties>  
    <skipTests>trueskipTests>  
properties>

或者

<properties>  
    <maven.test.skip>truemaven.test.skip>  
properties>  

2、如果是指定特定的特定的测试类时,此时需要使用maven-surefire-plugin这个插件,因为默认测试使用的就是这个插件进行关联。

官网:http://maven.apache.org/components/surefire/maven-surefire-plugin/

如下pom.xml,指定了测试类及排除某些类

复制代码
...
<
build> <plugins> org.apache.maven.plugins maven-surefire-plugin 2.20 **/*Tests.java **/Abstract*.java plugins> build>
...
复制代码

同样,如果不想指定以上的写法,可以直接在命令行上指定测试类

mvn test -Dtest=[ClassName]

提示:通过命令行就不需要配置pom.xml

还可以直接指定某个测试类的指定方法(注意:插件要2.8以上,所以还必须指定pom.xml的版本)

mvn test -Dtest=[ClassName]#[MethodName]
[MethodName]为要运行的方法名,支持*通配符,范例:
mvn test -Dtest=MyClassTest#test1
mvn test -Dtest=MyClassTest#*test*

你可能感兴趣的:(java)