1

Can I have a Gradle task where I can have both dependsOn and mustRunAfter to define the dependencies and the ordering?

tasks.register('MyTask') {
    dependsOn 'foo'
    dependsOn 'bar'
    dependsOn 'toy'
    
    tasks.findByName('bar').mustRunAfter('foo')
    tasks.findByName('toy').mustRunAfter('bar')
}

I want to run foo -> bar -> toy.

Are these two the same?

tasks.register('MyTask2') {
    dependsOn 'hello'
    tasks.findByName('MyTask2').mustRunAfter('hello')
}   


tasks.register('MyTask2') {
    mustRunAfter('hello')
}     

Is this a correct approach? I tried going over a few posts, but couldn't find an answer.

SyncMaster
  • 9,754
  • 34
  • 94
  • 137

1 Answers1

1

To order tasks you can use dependsOn or mustRunAfter. But there is a big difference between them

I want to run foo -> bar -> toy.

If you want to run a chain of tasks when you run one of them in the chain you need to use dependsOn:

tasks.register('MyTask') {
    // some logic is here
}    

// do not configure other tasks inside another task

tasks.findByName("bar").dependsOn("foo")
tasks.findByName("toy").dependsOn("bar")

The result will be as follows:

  • when you run toy, bar and foo will be run
  • when you run bar, foo will be run
  • when you run foo, no other tasks will be run

Are these two the same?

No, it's not the same thing. This means that when you run MyTask2, hello will be run erlier:

tasks.register('MyTask2') {
    dependsOn 'hello'
    // tasks.findByName('MyTask2').mustRunAfter('hello')
}

This means that when you run MyTask2, nothing will be happen:

tasks.register('MyTask2') {
    mustRunAfter('hello')
}  

But when you run MyTask2 and hello together, MyTask2 will be run only after hello.

There is an example that I posted on my github

Maksim Eliseev
  • 487
  • 1
  • 11