我可以使用dependsOn参数在插件中创建gradle任务吗?

时间:2017-09-25 04:53:01

标签: gradle build.gradle gradle-plugin

我正在创建一个gradle插件,如下所示

class MyPlugin implements Plugin<Project> {
    void apply(Project project) {
        project.extensions.create('testCoverageVerificationTask', TestCoverageVerificationPluginExtension)
        project.tasks.create('testCoverageVerification', TestCoverageVerificationTask)        
    }
}

要使用它,我需要将它们添加到我的build.gradle

apply plugin: my.package.MyPlugin

testCoverageVerificationTask {
    myreport = "testing report"
}

testCoverageVerification.dependsOn "myDependentTask"

但是,我认为将dependsOn作为testCoverageVerificationTask中的另一个参数会更好,因此不需要单独定义。那可行吗?

注意:正常的任务定义可以执行此操作

task myTask(dependsOn: 'compile') {
    doLast {
        println 'I am not affected'
    }
}

但我无法做到

testCoverageVerificationTask(dependsOn: "myDependentTask") {
    myreport = "testing report"
}

1 个答案:

答案 0 :(得分:1)

使用:

Task task = project.tasks.create('testCoverageVerification', TestCoverageVerificationTask)
task.dependsOn("compile")