How to debug a Gradle build.gradle file (in a debugger, with breakpoints)?

Is there a tool that will allow me to set breakpoints in a build.gradle file and step through tasks in a debugger?

Note: I believe that I'm asking a different question than similar stackoverflow questions about debugging Gradle plugins, where (presumably) the intent is to step through custom Groovy or Java plugin code located in a separate file. I want to set a breakpoint in a Gradle task in a simple build.gradle file, like...

task example {
   println "I want to set a breakpoint here"
}

...so that when I run gradle example I can inspect the context in a debugger.

(For those who would point me to IntelliJ...although JetBrains' website advertises that they support debugging Gradle scripts in IDEA UI, AFAICT this is untrue, as this was reported broken in IDEA13 EAP and hasn't been fixed in IDEA14. See Debugging Gradle build files in Intellij / Android Studio )

Is there any debugging tool that allows me to set a breakpoint in a build.gradle file, or is there something about the Gradle DSL that makes it fundamentally impossible to set breakpoints in a task such as my example, above?


There is the easier way:

just add in your command line -Dorg.gradle.debug=true --no-daemon

For example: gradle nameOfTask -Dorg.gradle.debug=true --no-daemon

Then you should start your IDE and run remote debugging with localhost port 5005, that all.

Gradle is waiting to you, because standard option server=y

org.gradle.debug

When set to true, Gradle will run the build with remote debugging enabled, listening on port 5005. Note that this is the equivalent of adding -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005 to the JVM command line and will suspend the virtual machine until a debugger is attached.

Link to docs


IntelliJ 2018.2 added the ability to debug Gradle scripts in a similar fashion to how you might run/debug other projects. You can see the announcement in the release notes here.

Here is a screenshot of some of the documentation from 2018.2:

IntelliJ documentation for debugging a Gradle script task

It does not yet support the kotlin-dsl (see gradle/kotlin-dsl/issues/39).