How to get custom task to run after a default task?

Hi,
I am trying to get a custom task to run after a built-in task, in my case the built in task is “wrapper” I cannot figure out how to get “wrapper” to run first at all. I have recreated a simple test that seems to work consistently for me and seems to conflict with what the docs say at https://docs.gradle.org/current/userguide/more_about_tasks.html#sec:ordering_tasks

We’re working in the Kotlin DSL so this is in our build.gradle.kts

tasks.register("hello") {
    dependsOn("wrapper")
    mustRunAfter("wrapper")
    exec {
        commandLine("ls")
    }
}

In this instance the “ls” shows that as of that command executing there is no gradlew anywhere in sight, but after the task is done it exists, meaning the ordering is wrong somehow. If anyone can point me in the right direction that would be fantastic.

Your task has no task action, the exec call is executed during configuration phase. Furthermore, the mustRunAfter is superfluous because dependsOn is much stronger. This is maybe what you want:

tasks.register("hello") {
    dependsOn("wrapper")
    doLast {
        exec {
            commandLine("ls")
        }
    }
}

Gradle Build Lifecycle