Ric Ambridge
Ric Ambridge

Reputation: 148

Running the same task multiple times during execution in gradle

Is it possible to have some like the following in gradle

task enableMe() << {   
   println "Enable"
}
task disableMe() << {
   shouldRunAfter 'taskAwork'
   println "Disable"
}

task taskAwork(){
   shouldRunAfter 'enableMe'
   println "do work in A while disabled"
}
task taskA(dependsOn: disableMe, taskAwork, enableMe){
}
task taskBwork(){
   shouldRunAfter 'enableMe'
   println "do work in B while disabled"
}
task taskB(dependsOn: disableMe, taskBwork, enableMe){
}

task taskC(dependsOn: taskA, taskB){
}

So that when it runs the tasks are executed in the order

disableMe
taskAwork
enableMe
disableMe
taskBwork
enableMe

but currently, disableMe and enableMe only run once.. is there anyway to set there status so that they can run again.

The only way I can think to do this is to duplicate the tasks;

task enableMeA() << {   
   println "Enable"
}
task disableMeA() << {
   shouldRunAfter 'taskAwork'
   println "Disable"
}
task enableMeB() << {   
   println "Enable"
}
task disableMeB() << {
   shouldRunAfter 'taskBwork'
   println "Disable"
}

task taskAwork(){
   shouldRunAfter 'enableMeA'
   println "do work in A while disabled"
}
task taskA(dependsOn: disableMeA, taskAwork, enableMeA){
}
task taskBwork(){
   shouldRunAfter 'enableMeB'
   println "do work in B while disabled"
}
task taskB(dependsOn: disableMeB, taskBwork, enableMeB){
}

task taskC(dependsOn: taskA, taskB){
}

Upvotes: 3

Views: 2564

Answers (2)

Stanislav
Stanislav

Reputation: 28106

Seems, in your case, you have to implement TaskExecutionListener for your task and use it's beforeExecute and afterExecute methods to implement your logic.

This could look something like:

task taskAwork() << {
    println "do work in A while disabled"
}
task taskBwork() << {
    println "do work in B while disabled"
}

task doAllWorkTask(dependsOn: [taskAwork, taskBwork]){
}

gradle.taskGraph.addTaskExecutionListener new WorkTasksExecutionListener()

class WorkTasksExecutionListener implements TaskExecutionListener {

    @Override
    void afterExecute(final Task task, final TaskState state) {
        if (task.name.contains('work')) {
            println('Enabling smth before execution of work task')
        }
    }

    @Override
    void beforeExecute(final Task task) {
        if (task.name.contains('work')) {
            println('Disabling smth after execution of work task')
        }
    }
}

Here is specified 2 tasks, which do something and requires some common loigic to be run before and after each one. To implement this common logic, used WorkTasksExecutionListener implementing TaskExecutionListener interface and registered as a listener in the taskGraph.

WorkTasksExecutionListener implenets 2 methods of the TaskExecutionListener: beforeExecute and afterExecute. This methods are getting called before and after each tsk execution, so in it's implementation was added the condition to check whether the task do some work and if yes, then some additional logic executed.

Output in this case will be:

:taskAwork
Enabling smth after execution of work task
do work in A while disabled
Disabling smth before execution of work task
:taskBwork
Enabling smth after execution of work task
do work in B while disabled
Disabling smth before execution of work task
:doAllWorkTask

Upvotes: 2

yonisha
yonisha

Reputation: 3096

Gradle, by default, will not run a task which is up-to-date. Task is considered up-to-date if it's TaskOutputs property hasn't been changed.

If you want to run a task several times during the same execution, you can use upToDateWhen property to define a predicate that will return false whenever you want the task to run again:

task myTask() {
    outputs.upToDateWhen {some_condition}

    // Do some stuff
}

Upvotes: 0

Related Questions