7

Now that IntelliJ 12.1 is out, I was hoping to be able to attach the debugger to a 'gradle run' app and have it stop at breakpoints. I've tried both right-clicking run and choosing to debug it, and setting GRADLE_OPTS environment variables as suggested in this answer:

Debug Gradle plugins with IntelliJ

and attaching the debugger remotely, which works fine, but neither one breaks on the breakpoints. I must be missing something.

Community
  • 1
  • 1
psugar
  • 1,897
  • 2
  • 18
  • 27

2 Answers2

14

I can debug remotely by configuring the run task. Since it is a JavaExec task, it supports jvmArgs properties:

run {
    jvmArgs "-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005"
}

and debug properties:

run {
    debug true
}

Right clicking to debug doesn't seem to work because IntelliJ is attaching the debugger to the wrong JVM i.e. gradle.

ceilfors
  • 2,617
  • 23
  • 33
  • Great! That works. For bonus points and eternal gratitude, is there a way to chain starting the run task and attaching the remote debugger, any idea? – psugar Jun 19 '13 at 17:39
  • I'm [afraid](http://stackoverflow.com/a/12454891/2464295) [no](http://stackoverflow.com/q/3823346/2464295), [because](http://stackoverflow.com/a/4558818/2464295) it's a child process. – ceilfors Jun 19 '13 at 23:32
  • 2
    Quick tips, you can use gradle property (-P) and make the build.gradle read the property to enable the debug e.g. if hasProperty -> debug true – ceilfors Jun 20 '13 at 02:23
  • Your solution is still valid. There had been some difficulties for me, however, getting it to work with the last versions of IntelliJ and Gradle using a debugging task. I've created a new post for it, if you're interested: http://stackoverflow.com/q/42748377/3767398 – matfax Mar 15 '17 at 14:00
  • I am not quite familiar with Gradle. I have the same issue that I cannot debug from the Intellij. Where should I add this run jvmArgs? Is it possible to add it to run configuration of Intellij gradle run task? – Shilan Apr 13 '23 at 11:42
2

I can debug normally. I only can't debug if I use the gradle daemon ("--daemon" option).

  • I found that Intellij automatically passes the required jvmArgs, so disabling the daemon (so the process started with these jvmArgs actually executes the code) does the trick for me. – mabi Nov 20 '13 at 08:36
  • Use --no-daemon to one-off disable the daemon btw. – Eric Wendelin Dec 14 '15 at 19:30
  • 2
    This argument does not work with the newest versions of IntelliJ anymore since it uses the Gradle Tooling API. There is no way to turn off the daemon. – matfax Mar 12 '17 at 11:47