When should one use dependsOn and when finalizedBy?

Reading documentation on gradle I have trouble finding best practices when one should use dependsOn and when finalizedBy .

I have the idea that one should prefer dependsOn if possible and only use finalizedBy if dependsOn doesn’t fit the requirements but I am not sure if that’s the intention.

There’s probably a couple of different ways to look at it. Given tasks a and b, I would want b to depend on a if there was a consumption relation between the two tasks (ie b consumes some output of a) or I wanted to be able to run a independent of b (ie I do “gradle a” and only the a task runs).

FinalizedBy is a little different in that it typically represents more of a helper relationship between the two. That is, if task a was finalized by task b, b might not necessarily consume something produced by a, but it might do something on behalf of a. In this case, I could not run a independently of b (i.e. I do a “gradle a” and both tasks a and b always run, even if task a fails). This is typically used for cleanup - for instance, stopping a test web server after running an integration test task (even if the integration test task fails).

I’d say that most of your task relationships are going to be dependsOn relationships. You’ll recognize the few where you might need/prefer a finalizedBy.