简体   繁体   中英

Add task dependency to existing Gradle task

I'm going to lose my mind about this. I have a build.gradle file that looks something like:

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

and I'm getting this:

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

I can't figure out what the right syntax is. Any help?

This should work:

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

Maybe my working example would be useful - fragments of build.gradle: (gradle version's 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'

I had a very similar error:

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.

My problem was that my task runEC2Step1DownloadSource was defined like this:

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

And the solution was to add an ARRAY to the list of dependsOn tasks:

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

Note the extra [] brackets around the list of dependsOn tasks.

I think you just want to add behavior after task finished,just get all task name and add hook,it will works.
Like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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