How to skip tests when building a project on Maven


内容纲要

How to skip tests when building a project on Maven

Basic Info

OS

cat /etc/os-release 

显示结果如下:

NAME="Ubuntu"
VERSION="18.04.1 LTS (Bionic Beaver)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 18.04.1 LTS"
VERSION_ID="18.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=bionic
UBUNTU_CODENAME=bionic

JDK

javac

javac -version

显示结果如下:

javac 1.8.0_181

java

java -version

显示结果如下:

java version "1.8.0_181"
Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)

Maven

mvn -v 

显示结果如下:

Apache Maven 3.5.4 (1edded0938998edf8bf061f1ceb3cfdeccf443fe; 2018-06-18T02:33:14+08:00)
Maven home: /opt/apache-maven-3.5.4
Java version: 1.8.0_181, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-8-oracle/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.15.0-33-generic", arch: "amd64", family: "unix"

Building a Maven project

本地开发时,想要提高项目的编译速度。通过查询Maven文档,可以在本地编译时指定跳过test生命周期。下面笔者会介绍两种跳过test生命周期的方法。

方法1: -Dmaven.test.skip

mvn clean install -Dmaven.test.skip

Ps: 此方法会跳过测试源码的编译,所以也不会执行测试代码。官方不推荐此方法。

方法2: -DskipTests

mvn clean install -DskipTests

Ps: 此方法会编译测试代码,但是不会执行测试代码。

方法3: -Dmaven.test.skip.exec

mvn clean install -Dmaven.test.skip.exec=true

三者的区别

参数 生命周期 所属插件 相同点 不同点
maven.test.skip compile compiler:compile 不执行测试代码 不编译测试代码
skipTests test surefire:test 不执行测试代码 编译测试代码
maven.test.skip.exec compile compiler:compile 不执行测试代码 编译测试代码

如何找到这些属性的?

首先,我们需要了解一个Maven项目的全生命周期,关于全生命周期请阅读参考文档1。通过文档我们可以知道如下几点:

  1. compile生命周期: compile the source code of the project.
  2. test生命周期: run tests using a suitable unit testing framework. These tests should not require the code be packaged or deployed.
  3. compile生命周期绑定插件为: compiler:compile
  4. test生命周期绑定插件为: surefire:test

接下来,我们在终端下查看这两个插件的帮助文档,看如何跳过测试代码执行。

compiler

mvn compiler:help -Ddetail=true | grep -i skip -A 4

显示结果如下:

    skipMain
      Set this to 'true' to bypass compilation of main sources. Its use is NOT
      RECOMMENDED, but quite convenient on occasion.
      User property: maven.main.skip

    skipMultiThreadWarning (Default: false)

      User property: maven.compiler.skipMultiThreadWarning

    source (Default: 1.6)
      The -source argument for the Java compiler.
      NOTE: Since 3.8.0 the default value has changed from 1.5 to 1.6
--
    skip
      Set this to 'true' to bypass compilation of test sources. Its use is NOT
      RECOMMENDED, but quite convenient on occasion.
      User property: maven.test.skip

    skipMultiThreadWarning (Default: false)

      User property: maven.compiler.skipMultiThreadWarning

    source (Default: 1.6)
      The -source argument for the Java compiler.
      NOTE: Since 3.8.0 the default value has changed from 1.5 to 1.6
--
[INFO] CollectTomcatLogs 1.0-SNAPSHOT ..................... SKIPPED
[INFO] TomcatLogsAnalysis 1.0-SNAPSHOT .................... SKIPPED
[INFO] ScalaReadAndWrite 1.0-SNAPSHOT ..................... SKIPPED
[INFO] weblogsanalysissystem 1.0.0-SNAPSHOT ............... SUCCESS [  1.428 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

surefire

mvn surefire:help -Ddetail=true | grep -i skip -A 4

显示结果如下:

    skip (Default: false)
      Set this to 'true' to bypass unit tests entirely. Its use is NOT
      RECOMMENDED, especially if you enable it using the 'maven.test.skip'
      property, because maven.test.skip disables both running the tests and
      compiling the tests. Consider using the skipTests parameter instead.
      User property: maven.test.skip

    skipAfterFailureCount (Default: 0)
      Set to error/failure count in order to skip remaining tests. Due to race
      conditions in parallel/forked execution this may not be fully guaranteed.
      Enable with system property -Dsurefire.skipAfterFailureCount=1 or any
      number greater than zero. Defaults to '0'.
      See the prerequisites and limitations in documentation:
      http://maven.apache.org/plugins/maven-surefire-plugin/examples/skip-after-failure.html
      User property: surefire.skipAfterFailureCount

    skipExec
      Deprecated. Use skipTests instead.

      This old parameter is just like skipTests, but bound to the old property
      'maven.test.skip.exec'.
      User property: maven.test.skip.exec

    skipTests (Default: false)
      Set this to 'true' to skip running tests, but still compile them. Its use
      is NOT RECOMMENDED, but quite convenient on occasion.
      User property: skipTests

    suiteXmlFiles
      (TestNG) List of <suiteXmlFile> elements specifying TestNG suite xml file
      locations. Note that suiteXmlFiles is incompatible with several other
--
[INFO] CollectTomcatLogs 1.0-SNAPSHOT ..................... SKIPPED
[INFO] TomcatLogsAnalysis 1.0-SNAPSHOT .................... SKIPPED
[INFO] ScalaReadAndWrite 1.0-SNAPSHOT ..................... SKIPPED
[INFO] weblogsanalysissystem 1.0.0-SNAPSHOT ............... SUCCESS [  0.896 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

The end

好了,以上就是我找到相关帮助文档的方法,希望大家也在终端尝试看看。Have fun~

Reference

  1. Introduction to the Build Lifecycle See Lifecycle Reference and Built-in Lifecycle Bindings
  2. Maven中-DskipTests和-Dmaven.test.skip=true的区别

声明:
  未经特别说明,本站Blog均采用署名-非商业性使用-禁止演绎 2.5 中国大陆授权。任何违反本协议的行为均属于非法行为。如需非商业性转载,请保留署名。如需商业性转载出版,请直接和联系。

,

发表回复

您的电子邮箱地址不会被公开。