简体   繁体   English

Gradle 覆盖默认检查任务

[英]Gradle override the default check task

I've defined my sourceSets as我已将我的 sourceSets 定义为

sourceSets {
    // Configuring SourceSets for all connector source files.
    main {
        java {
            srcDirs = ['src']
        }
    }
    test {
        // integrationTests
    }
    unitTests {
        //unitTests
    }
}

test {
   // integrationTests
}

task('unitTest', type: Test) {
  //unitTests
}

When I kick off ./gradlew build it's check task basically calls the test target.当我启动./gradlew build时,它的check任务基本上调用了test目标。

Can I override the default gradle build tasks's 'check' mechanism to call unitTest here instead of test ?我可以覆盖默认的gradle build任务的“检查”机制以在此处调用unitTest而不是test吗?

First of all, please consider just using the task test for unit tests and using another source set for integration tests.首先,请考虑仅将任务test用于单元测试并使用另一个源集进行集成测试。 Gradle and its plugins follow an approach called convention over configuration and the convention of the Java plugin is to use the task test for unit tests. Gradle 及其插件遵循称为约定优于配置的方法,并且 Java 插件的约定是使用任务test进行单元测试。


To actually answer your question, you can access and modify the dependencies of each task using getDependsOn() and setDependsOn(Iterable<?>) , so a simple solution to your problem could be the following code:要真正回答您的问题,您可以使用getDependsOn()setDependsOn(Iterable<?>)访问和修改每个任务的依赖关系,因此您的问题的简单解决方案可能是以下代码:

check {
    dependsOn.clear()
    dependsOn unitTest
}

This removes all task dependencies from the task check and then adds a dependency on the task unitTest .这会从任务check中删除所有任务依赖项,然后添加对任务unitTest的依赖项。 However, this may cause new problems, because other plugins may automatically register task dependencies for check that will be removed, too.但是,这可能会导致新的问题,因为其他插件可能会自动注册任务依赖项以进行check ,也将被删除。

An obvious improvement to the code above would be to just remove the dependency on the test task instead of all task dependencies.对上面代码的一个明显改进是只删除对test任务的依赖而不是所有任务依赖。 Sadly, this is not as simple as one would think.可悲的是,这并不像人们想象的那么简单。 You may have noticed that the method getDependsOn() returns a Set<Object> and not a Set<Task> .您可能已经注意到方法getDependsOn()返回一个Set<Object>而不是Set<Task> For convenience, task dependencies may be expressed not only using tasks but also by passing other objects that will resolve to tasks later on (eg a string containing the name of a task).为方便起见,任务依赖关系不仅可以使用任务来表达,还可以通过传递稍后将解析为任务的其他对象(例如,包含任务名称的字符串)来表达。 The current version of the Java plugin uses a Provider<Task> to register the dependency between the task check and the task test . 当前版本的 Java 插件使用Provider<Task>来注册任务check和任务test之间的依赖关系。 To just remove the dependency on the task test , you would need to iterate over all dependencies in dependsOn and find the one with the type Provider that returns the task test via its get() method.要仅删除对任务test的依赖,您需要遍历dependsOn中的所有依赖项,并找到类型为Provider的依赖项,该类型通过其get()方法返回任务test You could then remove that Provider from dependsOn .然后,您可以从dependsOn中删除该Provider

As you can see, it is possible to remove task dependencies once they have been registered, but it is not that easy, so you should probably just follow the convention and use the task test for unit tests.如您所见,一旦注册了任务依赖项,就可以删除它们,但这并不容易,因此您可能应该遵循约定并使用任务test进行单元测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM