gradle编译时跳过test测试

使用gradle编译项目时,若指定了测试目录,则进行gradle build时会在编译完成后自动执行gradle test任务。

sourceSets {
    main {
        java.srcDirs = ['src']
    }
    test {
        java.srcDirs = ['test']
    }
}

test {
    include '**/Test*.class'
    exclude '**/TestDataFetchers.class'
}

但是如果我不想build时自动执行测试,而是手动执行测试呢?我可不想在生产环境执行测试代码。
很简单,只要在部署脚本使用-x test就可以了。

gradle build -x test

手动执行test

gradle test

下面这种方法是不可取的,不仅build不会执行test,手动执行也不会执行

buildscript {
    ext.kotlin_version = '1.3.10'

    repositories {
        maven {url 'http://maven.aliyun.com/nexus/content/groups/public/'}
        mavenCentral()
    }

    dependencies {
        classpath "com.github.jengelman.gradle.plugins:shadow:2.0.3"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }

    //skip Test tasks
    gradle.taskGraph.whenReady {
        tasks.each { task ->
            if (task.name.contains("test"))
            {
                task.enabled = false
            }
        }
    }
}
> Task :testClasses SKIPPED
Skipping task ':testClasses' as task onlyIf is false.
:testClasses (Thread[Task worker for ':',5,main]) completed. Took 0.0 secs.
:test (Thread[Task worker for ':',5,main]) started.

> Task :test SKIPPED
Skipping task ':test' as task onlyIf is false.
:test (Thread[Task worker for ':',5,main]) completed. Took 0.0 secs.


    原文作者:鹅鹅鹅_
    原文地址: https://www.jianshu.com/p/61c5d163a4c9
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞