How do you attach a debugger to gradle so that I can debug it running a task?

Hi,

I want to attach a debugger so that I can debug through eclipse what gradle is doing.

However there seems to be no documentation on how to do this.

All I have found is this from http://www.gradle.org/java_plugin

Setting a system property of taskName.debug will run the tests in debug mode, suspended and listening on port 5005. For example: gradle test -Dtest.single=ThisUniquelyNamedTest -Dtest.debug

but that doesn’t seem to work for arbitrary tasks. e.g. gradle tasks -Dtasks.debug

Any pointers would be appreciated.

Cheers,

Stefan

2 Likes

Just debug it like you would debug any external Java application. Depending on which debug settings you want, put something like the following into GRADLE_OPTS:

-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5006

Then start the build and debug it from Eclipse.

1 Like

Awesome thanks.

Is there any way to get eclipse to recognize break points in .gradle files?

I, too, want to debug into Gradle code to better understand test report generation. I was having problems getting GRADLE_OPTS to propagate to spawned JVMs (I must have been doing something wrong). By that I mean that the JVM that handles the command line invocation of Gradle is not the same one that handles running your tests, and I could not get my debugger client (IntelliJ) to stop at a breakpoint in Gradle code in the test-running VM.

Here is how I solved it. Add the JVM debugging config to $HOME/.gradle/gradle.properties like this:

$ cat ~/.gradle/gradle.properties
org.gradle.daemon=true
org.gradle.jvmargs=-XX:MaxPermSize=4g -XX:+HeapDumpOnOutOfMemoryError -Xmx4g -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5006

Do something that starts the daemon:

$ gradle --daemon

Then attach your debugger client to port 5006, set your breakpoint, then run your test.

Works for me.

1 Like

Thanks, Peter. That -Dorg.gradle.debug=true property is good to know, and was what I was looking for.

Maybe that property would be a good one to mention here:

http://www.gradle.org/docs/current/userguide/build_environment.html

?

1 Like

It would certainly be useful, but I think it’s not considered public.

Got it.

I think it should be public. Either that, or go with my solution. I like yours better.

The:

gradle someTask --no-daemon -Dorg.gradle.debug=true does let me connect to gradle from the eclipse debugger, but still ignored breakpoints.

Anything special needed in the eclipse debug task config?

So what are the correct steps in order to debug a gradle script ?

See my post above. Setting breakpoints in build scripts isn’t currently supported, but you should be able to set breakpoints in plugin or task classes.

1 Like

Some more information on this would definitely be helpful. I’ve mad mixed results with eclipse & gradle so far.

I have been able to debug java/groovy that’s being called from gradle when running on anther system and using eclipse’s remote java app debug config combined with the GRADLE_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5006"

Note that this needs to be set in the env, it’s not picked up from ~/.gradle/gradle.properties as documented. Maybe I’m doing something wrong, but this is ignored: org.gradle.jvmargs="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5006"

I’ve had mixed results being able to stop on breakpoints when running as a remote java app. It’s probably related to where eclipse is looking for src/class files and maybe I don’t have that setup exactly right.

What I’d like to do is run gradle directly from eclipse directly and be able to hit breakpoints in my buildSrc/**/src/main/java classes.

I did the eclipse import gradle project for my project, and it recognizes it as a gradle project, but I don’t see how to run/debug it from eclipse.

1 Like

I just tried this one, and it worked out of the box:

http://blogs.steeplesoft.com/posts/2013/09/10/gradle-tip-attaching-a-debugger/

It can be done by adding -Dtest.debug, e.g. gradle test -Dtest.single=ThisUniquelyNamedTest -Dtest.debug. See http://www.gradle.org/docs/1.11/userguide/java_plugin.html

1 Like

How would I configure Eclipse to debug a Gradle script such that I can put breakpoints in Gradle’s groovy and java source files?

‘GRADLE_OPTS’ is only used by the Gradle (client) JVM. Your code should attach a debugger to the daemon JVM, not the test JVM.

The easiest way to debug a Gradle build is ‘gradle someTask --no-daemon -Dorg.gradle.debug=true’. Tests can be debugged with ‘gradle test -Dtest.debug=true’.

1 Like