What's the difference between mustRunAfter and dependsOn in Gradle?

For example:

tasks.create('a')

tasks.create('b').dependsOn('a')

tasks.create('c')

tasks.create('d').mustRunAfter('c')
  • dependsOn - sets task dependencies. Executing b here would require that a be executed first.
  • mustRunAfter - sets task ordering. Executing d does not require c. But, when both c and d are included, c will execute before d.

Sometimes they have the same effect. For example, if taskC dependsOn taskA and taskB, then it doesn't matter whether taskB dependsOn taskA or mustRunAfter it - when you run taskC, the order will be taskA, taskB, taskC.

But if taskC dependsOn taskB only, then there's a difference. If taskB dependsOn taskA, then it's the same as above - taskA, taskB, taskC. If taskB merely mustRunAfter taskA, then taskA doesn't run, and running taskC will run taskB, then taskC.

mustRunAfter really means if taskA runs at all, then taskB must run after it.