If you need to execute some tasks in predefined order, then you need to not only set dependsOn, but also to set mustRunAfter property for this tasks, like in the following code:
Here is how I did it, with Kotlin scripting, using both dependsOn and mustRunAfter. Here is an example of running two tasks, one (custom registered "importUnicodeFiles" task) that is in "this" project and one (predefined "run" task) that is in a sibling project named ":unicode":
tasks.register("rebuildUnicodeFiles") {
description = "Force the rebuild of the `./src/main/resources/text` data"
val make = project(":unicode").tasks["run"]
val copy = tasks["importUnicodeFiles"]
dependsOn(make)
dependsOn(copy)
copy.mustRunAfter(make)
}
The Gradle developers generally advise against this approach (they say that forcing ordering is bad, and that executing tasks from other projects is bad), and are working on a way to publish results between projects; see: https://docs.gradle.org/current/userguide/cross_project_publications.html
dependsOn causes the other tasks to run when cleanBuildPublish is run and the helper function placeTasksInOrder place the tasks in order by calling mustRunAfter
When executing gradlew cleanBuildPublish somethingElse the order will be clean, build, publish, somethingElse
Beware:
The following solution does NOT work if you have nested gradle calls
When executing gradlew cleanBuildPublish somethingElse, then it could happen, that somethingElsewill be called first.
So one should better use the mustRunAfterconfiguration.