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

来源:互联网 发布:linux nodejs 安装 编辑:程序博客网 时间:2024/06/16 00:24

转自: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.plugins</groupId>      <artifactId>maven-surefire-plugin</artifactId>      <version>2.20</version>      <configuration>          <skipTests>true</skipTests>      </configuration>  </plugin>  
复制代码

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

通过<properties>节点配置属性

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

或者

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

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

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

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

复制代码
...
<
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#test1mvn test -Dtest=MyClassTest#*test*