将任务依赖性添加到现有Gradle任务

时间:2014-02-26 21:46:14

标签: java groovy intellij-idea gradle

我会对此失去理智。我有一个build.gradle文件,看起来像:

apply plugin: 'idea'
task blah{
  // do something
}
idea{
  // some stuff
  dependsOn blah
}

我得到了这个:

Could not find method dependsOn() for arguments [task ':blah'] on root project ...

我无法弄清楚正确的语法是什么。有什么帮助吗?

4 个答案:

答案 0 :(得分:28)

这应该有效:

apply plugin: 'idea'
task blah{
  // do something
}
tasks.idea.dependsOn(blah)

答案 1 :(得分:2)

也许我的工作示例很有用 - build.gradle的片段: (gradle版本1.6)

ear {

    doFirst {
        tasks.buildWar.execute();
    }

    ...

}

task deployProj <<{
    tasks.ear.execute()
    tasks.copyEar.execute()
    tasks.copyJar.execute()
}

task buildWar(type: GradleBuild) {
    buildFile = 'mysubproject/build.gradle'
    tasks = ['war']
}

task copyEar(type: Copy) {
    from earPath
    into "$System.env.JBOSS_HOME" + deploymentPath
}

task copyJar(type: Copy) {
    from jarPath
    into libPath
}

copyEar.mustRunAfter 'ear'
copyJar.mustRunAfter 'ear'

答案 2 :(得分:0)

我有一个非常相似的错误:

Could not find method runEC2Step1DownloadSource() for arguments [{dependsOn=task ':docker:buildCopyEC2BuildFiles'}, task ':docker:runEC2SetupVariables', build_cwq7epb31twoanyxdhq1zosc7$_run_closure16@2a0fe301] on project ':docker' of type org.gradle.api.Project.

我的问题是我的任务runEC2Step1DownloadSource的定义如下:

task runEC2Step0CreateMachine(dependsOn: buildCopyEC2BuildFiles, runEC2SetupVariables) {
  doLast {
    // A bunch of stuff
  }
}

解决方案是将一个ARRAY添加到DependsOn任务列表中:

task runEC2Step0CreateMachine(dependsOn: [buildCopyEC2BuildFiles, runEC2SetupVariables]) {
  doLast {
    // A bunch of stuff
  }
}

请注意DependsOn任务列表周围的[]括号。

答案 3 :(得分:0)

我认为您只是想在任务完成后添加行为,只需获取所有任务名称并添加钩子即可。
像这样:

    tasks.whenTaskAdded {
        theTask ->
            if (theTask.name.contains('externalNativeBuild')) {
                theTask.doLast{
                    println "[*] begin to copy file!"
                }
            }
//            println theTask.name
    }