Debugging a Gradle Spring Boot Application in Intellij

Out the box Spring Boot integration with Intellij (for the most part) is great. Debugging integration tests seem to work flawlessly with zero configuration changes to either Intellij and Gradle. Where it gets frustrating is in Spring/Gradle’s lack of support for debugging a spring boot app running under ./gradlew bootRun.

For those developing under maven, the spring-boot maven plugin docs are quite clear as to how to achieve this – simply add the jvmArguments with your debug properties and you’re up and running. Not quite as easy with the spring gradle plugin.

The following trick is what I use in order to debug my spring-boot apps. First we update our gradle task to inject our debug JVM properties only when the bootRun task is called.

// -------------------------------------
// BOOT RUN DEBUG
// -------------------------------------
tasks.withType(org.springframework.boot.gradle.run.BootRunTask) {
    if (System.getProperty('debug', 'false') == 'true') {
        jvmArgs = ["-Xmx2g", "-Xdebug", "-XX:+CMSClassUnloadingEnabled",
                   "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5105"]
    }
}

And from the command line, running ./gradlew -ddebug=true bootRun will spin up the spring-boot app listening on 5105.

Next up, we simply attach onto the debug port by creating a Remote Application Run configuration in Intelllij.

And voila. Start your application as described earlier, then in IntelliJ select the new configuration and hit Debug. IntelliJ will connect to the JVM and initiate remote debugging.

Happy debugging.

Update 30/08/2017:

Shortly after publishing this post I stumbled across the following stack-overflow answer. With that in mind I’ve re-tweaked my gradle build to the following:

// -------------------------------------
// BOOTRUN DEBUG
// -------------------------------------
bootRun {
    if (System.getProperty('debug', 'false') == 'true') {
        jvmArgs = ["-Xmx2g", "-Xdebug", "-XX:+CMSClassUnloadingEnabled",
                   "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5105"]
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *