Home > other >  How to run a command after a gradle task is killed?
How to run a command after a gradle task is killed?

Time:11-10

I have the following gradle task that works fine:

abstract class StartDevTask : DefaultTask() {

    private val developmentTasks = listOf(
        "dev-db",
        "run",
        "watch"
    )

    @TaskAction
    fun startDevelopmentTasks() {
        developmentTasks
            .map { ProcessBuilder("./gradlew", it).start() }
            .map {
                Thread { it.waitFor() }.also { it.start() }
            }
            .map { it.join() }

        println("All processes finished")

    }
}

// registering the task over here
tasks.register<StartDevTask>("start-dev") 

Then I can Invoke from the command line:

$ ./gradlew start-dev

Which works just fine, but when I kill the process with Ctrl C and check how many processes are alive with:

$ ps -ax | grep gradlew

I can see a lot of those leftover processes, to remove them I can just run ./gradlew --stop

My question is; how can I run ./gradlew --stop after that task is killed? Or can someone hint me to a better alternative of doings this?

Thanks!!

CodePudding user response:

The leftover processes are Gradle daemon processes. Gradle uses these long-lived processes to speed up future builds by reusing cached computations.

If you really don't want them to hang around, you can run Gradle with the --no-daemon flag to tell it not to leave the daemon process running.

For example:

 ./gradlew start-dev --no-daemon
  • Related