how to debug spring application with gradle
Solution 1
After you run gradle bootRun --debug-jvm
the application is suspended until you connect your debugger to the port it is listening on (port 5005).
Solution 2
As a response to dankdirkd's answer above: (compare)
gradle bootRun --debug-jvm
will make the gradle build run in debug mode. That probably is not what you want. What you want to achieve is that the springBoot task starts your application in debug mode.
The spring boot task extends the gradle JavaExec task. You can configure the bootRun task in your build.gradle file to add a debug configuration like this:
bootRun {
jvmArgs=["-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=32323"]
}
For the build.gradle.kts this would look like this (with suspend mode disabled):
tasks {
val bootRun by getting(BootRun::class) {
jvmArgs=listOf("-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=32323")
}
}
If your server is within a cloud and you want to debug from local machine, you need to make sure that it allows connections from outside. Use below configuration in that case
tasks {
val bootRun by getting(BootRun::class) {
jvmArgs=listOf("-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=0.0.0.0:32323")
}
}
Note that the address is now 0.0.0.0:port
instead of just port
Solution 3
For build.gradle.kts
file you can also simply use below:
tasks.withType<BootRun> {
jvmArgs = listOf("-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=0.0.0.0:32323")
}
Solution 4
I personally prefer going under Gradle tasks
and right-clicking on the bootRun
. This is useful in the IDE compared to the terminal.
rigby
Updated on July 09, 2022Comments
-
rigby almost 2 years
I am working on spring app and need to step through a controller method to see how it works. I am working in eclipse and building my app with
gradle bootRun
command.How to run it in debug mode?
i tried
gradle bootRun --debug
but it's just debug log, not debug modei tried
gradle help --task bootRun
-- this gives me info about task there i saw--debug-jvm
option but when i rungradle bootRun --debug-jvm
application doesn't start -
Pete_ch over 7 yearshaving an issue running gradle bootRun --debug-jvm. hangs at Building 96% > :<project>:bootRun
-
Neal.Shan almost 7 yearsthis is the right answer, suspended means you need to click your remote debug button to connect to listener and then app start :P
-
hariharan kumar over 6 yearsBy default, the debug port is 5005. How to start from a custom port say 5006?
-
AlikElzin-kilaka about 6 yearsAlso, how to start without suspend?
-
Jazzepi almost 6 yearsI don't believe that --debug-jvm debugs the main gradle process. The bootRun process is a subclass of JavaExec which starts a child process in debug mode when receiving the --debug-jvm flag. docs.gradle.org/current/dsl/…
-
Lucas Basquerotto over 5 years@hariharankumar I haven't tested with
bootRun
specifically, but theBootRun
task extendsJavaExec
(github.com/spring-projects/spring-boot/blob/master/…), and I made it work withJavaExec
passingjvmArgs
to the task, so you could apply a similar logic: stackoverflow.com/a/53946140/4850646 -
Nick Wills about 4 yearsThanks. This indeed can allow developer to leaverage on breakpoint and trace thru codes. However, is there a way to combine both steps (run apps and attach debugger) Ina single execution of command or a button click. Cos Everytime when we make java code change, we have to stop the above process. Then execute these 2 steps manually. Seem like a chores to me.
-
Nick Wills over 2 yearsBut how do u use Gradle tasks to run an application in debug mode? I also don't like the idea of using temrinal
-
Nick Wills over 2 yearsSo i just add bootRun { jvmArgs=["-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=32323"] } into the build.gradle. And that's it?