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

一、过滤整个测试代码,能够直接在命令行上指定java

mvn clean install -Dmaven.test.skip=true

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

或者maven

mvn clean install -DskipTests

还能够直接在pom.xml文件上指定,好比使用maven-surefire-plugin时的配置测试

<plugin>  
    <groupId>org.apache.maven.plugins</groupId>  
    <artifactId>maven-surefire-plugin</artifactId>  
    <version>2.20</version>  
    <configuration>  
        <skipTests>true</skipTests>  
    </configuration>  
</plugin>  

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

经过<properties>节点配置属性spa

<properties>  
    <skipTests>true</skipTests>  
</properties>

或者插件

<properties>  
    <maven.test.skip>true</maven.test.skip>  
</properties>  

二、若是是指定特定的特定的测试类时,此时须要使用maven-surefire-plugin这个插件,由于默认测试使用的就是这个插件进行关联。命令行

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

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

...
<
build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.20</version> <configuration> <!-- 包含 --> <includes> <include>**/*Tests.java</include> </includes> <!-- 排除 --> <excludes> <exclude>**/Abstract*.java</exclude> </excludes> </configuration> </plugin> </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*
相关文章
相关标签/搜索