Debugging Gradle tasks from Eclipse with one click: a plugin built on Buildship

In this 2016 thread, debugging Gradle tasks from Buildship was on the roadmap; the workaround is still --debug-jvm plus a Remote Java Application in Eclipse. I wrote an open-source plugin that does it in one click, and I would like feedback from Buildship users and maintainers.

What it does. It adds Debug Gradle Tasks to the context menu of the Gradle Tasks view and a debug mode to Gradle Task run configurations. Every JVM the build forks (JavaExec, Test, bootRun, …) is attached to the Eclipse debugger.

How it works.

  • Eclipse listens with JDT’s Socket Listen connector on a free port.
  • An init script, added through Buildship’s invocationcustomizers extension point, sets debugOptions on every JavaForkOptions task: server=false, suspend=true, the port read lazily from a system property, so the configuration cache is reused across debug sessions.
  • During the session, a saved Java file is compiled by Gradle (classes) and the class files it rewrote go to JDT’s hot code replace. Classes compiled by ECJ cannot replace classes compiled by javac: their lambda methods have different names.

Two questions for the Buildship team.

  1. Reading the selection of the Gradle Tasks view needs Buildship’s internal node classes, which org.eclipse.buildship.ui does not export. Would a small public API for the selected tasks be welcome?
  2. Buildship runs builds inside IWorkspace.run, which holds back Eclipse’s auto-build until a long-running task such as bootRun ends. Is that intended for run configurations?

GitHub: GitHub - legrottagliegionata/eclipse-gradle-debug: Debug any Gradle task from Eclipse's Gradle Tasks view, as in IntelliJ IDEA · GitHub (EPL 2.0, Eclipse 2024-06 or later, also on the Eclipse Marketplace). Write-up with the details: Debugging Gradle tasks in Eclipse, the way IntelliJ does it - DEV Community

Disclosure: I am the author of the plugin.

With server=false the suspend option is ignored as far as I’m aware, I’d remove it.

Thanks for looking at it! I checked, and suspend is honored with server=n too. The JDWP agent connects to the debugger during JVM startup, and with suspend=y the VM start event suspends all threads until the debugger resumes them.

A quick test with jdb -connect com.sun.jdi.SocketListen:port=… and a hello-world:

- server=n,suspend=y: main only ran after I typed cont in jdb.

- server=n,suspend=n`: main ran and the JVM exited right after connecting, before the debugger could do anything.

The plugin relies on that. Eclipse installs the breakpoints while the forked JVM is suspended at start, so a breakpoint on the first lines of main is still hit. With suspend=false that would be a race.

Oh, interesting.
Wasn’t aware of that.
What I always observed was, that with server=n the JVM startup fails when the debugger is not listening.
So I assumed that suspend is ignored because if it couldn’t connect JVM startup fails and if it was able to connect it continued immediately.
That there are underlying things done by the debugger first and then threads resumed was never visible to me.

Thanks for telling, TIL.