How to debug SpringBoot using Gradle commandline parameter, without suspending server?

Viewed 1294

I'd like to start my SpringBoot application, on debug mode, using Gradle (to use on my machine the same command that is used on production)

To start my server I use

$ gradle bootRun

And I know that I can add a parameter to start on debug mode

$ gradle bootRun --debug-jvm

The problem is that the above command makes the server suspends (the server is not started until I connect the debugger)

I can have the desired behavior by adding the following code to the build.gradle, however, I'd like to use a command-line parameter, to avoid to commit the build.gradle by mistake.

bootRun {
   jvmArgs(['-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005'])
}

Note: If I add the following parameters I'll be debuging the gradle daemon (or another gradle thing), not my app, so it's not a valid solution also:

gradle bootRun -Dorg.gradle.jvmargs="-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005"

Note 2: the following code doesn't work - the server starts on non-debug mode

gradle bootRun -Dagentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005

Related: how to debug spring application with gradle

1 Answers

You are basically asking how to pass variables to your project.

Gradle has many ways of doing this. I prefer the project properties approach.

Create an environmental variable like:

ORG_GRADLE_PROJECT_debug_jvm=-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005

gradle.build

bootRun {
   jvmArgs project.findProperty('debug_jvm') ?: ''
}

See also: Pass env variables to gradle.properties

Related