Properly stop a running gradle process

Hi!

I use gradle to execute my Java apps.

After going the normal clean and compile steps, I use the following task to kickstart my Java app:

task(runMain, dependsOn: ‘classes’, type: JavaExec) {
main = “$mainClassName”
classpath = sourceSets.main.runtimeClasspath
args ‘’
}

What is the proper way to stop my java/gradle process without killing the gradle daemon?

At the moment I have a custom script which kills the Java process but is there a less hacky way of achieving what I want?

If I use gradle --stop, the daemon is also stopped.

I think that depends on your operating system and also on your application

If you are using spring boot you could hit the /shutdown endpoint (more info here). Or your app could have a custom shutdown endpoint of it’s own.

If using linux you could use ps -ef | grep MyMainClass to find the process id (pid) and then kill the pid

On windows there’s less detail given for each process so you end up picking a java.exe task in Task Manager and killing it, you might use the memory & cpu usage as a guide if there’s more than one java.exe process.