Gradle 4 and task dependencies


(sergey.morenets) #1

Hi

What is the optimal way to specify dependencies between tasks?
I have a multi-project build with task:

task buildFrontEnd(type: Exec) {
commandLine ‘cmd’, ‘/c’, 'ng build --dev’
workingDir ‘src/main/webapp’
}

I want to execute it after the successful build and these approaches don’t execute this task:

buildFrontEnd.mustRunAfter ‘assemble’

assemble.doLast { buildFrontEnd }

task buildFrontEnd(type: Exec, dependsOn: ‘assemble’) {

The only working approach is: assemble.doLast { buildFrontEnd.execute() }

Is it correct way? Please advise.
Thanks.


(Pierre) #2

Hi,

If you want to run after a particular task - let’s say assemble - for each project in your build, I’d use finalizedBy:

task buildFrontEnd {
// ...
}
assemble.finalizedBy buildFrontEnd

If you want to run it only once after all projects have been built, I’d stick a listener in the lifecycle:

project.gradle.buildFinished { BuildResult r ->
    if (r.failure == null) {
        // Trigger command
    } else {
        // when build has failed...
    }
}

In the latter case you cannot use a task. As far as I know, using a task and calling the method annotated TaskAction is not recommended.