Gradle Tip: Attaching a Debugger - No Fluff Just Stuff

Gradle Tip: Attaching a Debugger

Posted by: Jason Lee on September 10, 2013

Maven offers a nice script to allow for attaching a debugger to your build, mvnDebug. Gradle does not. Again, though, Gradle makes it pretty easy to add this to your build.

pass::[more]

Let’s say you want to debug your tests:

build.gradle
test {
          if (System.getProperty('DEBUG', 'false') == 'true') {
              jvmArgs '-Xdebug',
                  '-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=9009'
          }
      }

From the command line, issue gradle -DDEBUG=true test:

$ gradle -DDEBUG=true test
      :compileJava UP-TO-DATE
      :processResources UP-TO-DATE
      :classes UP-TO-DATE
      :compileTestJava
      :processTestResources UP-TO-DATE
      :testClasses
      :test
      Listening for transport dt_socket at address: 9009
      > Building > :test

When you see that line, you can attach the debugger of your choice, using port 9009. This also works if you’re building a command line application:

build.gradle
run {
          if (System.getProperty('DEBUG', 'false') == 'true') {
              jvmArgs '-Xdebug',
                  '-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=9009'
          }
      }

and run:

gradle -DDEBUG=true run
      :compileJava UP-TO-DATE
      :processResources UP-TO-DATE
      :classes UP-TO-DATE
      :run
      Listening for transport dt_socket at address: 9009
      > Building > :run

To add this all of your projects, you can make this change to init.gradle:

~/.gradle/init.gradle

allprojects {
          tasks.withType(Test) {
              if (System.getProperty('DEBUG', 'false') == 'true') {
                  jvmArgs '-Xdebug',
                      '-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=9009'
              }
          }
      
          tasks.withType(JavaExec) {
              if (System.getProperty('DEBUG', 'false') == 'true') {
                  jvmArgs '-Xdebug',
                      '-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=9009'
              }
          }
      }
Jason Lee

About Jason Lee

Jason Lee is a Senior Java Developer for Sun Microsystems working on the GlassFish Administration Console, and is a member of the JSF 2.0 (JSR 314) Expert Group. Jason has extensive experience working with web-based technologies such as JavaServer Faces and Ajax, as well as enterprise technologies based on the GlassFish platform. He is currently the main developer of Mojarra Scales, working to create a set of high quality JSF components wrapping libraries such as the Yahoo! User Interface Library, as well as bring Facelets compatibility to JSFTemplating.

Jason has been writing software professionally since 1997 in a wide variety of languages and environments, including Java, PHP, C/C++, and Delphi on both Linux/Unix and Windows. You can read more about what Jason's working on at his blog at http://blogs.steeplesoft.com

Apart from work, he is currently serving as the president of the Oklahoma City Java Users Group, where he is an active member and presenter. More importantly, Jason is married to a beautiful woman and has two sons who, thankfully, look like their mother.

Why Attend the NFJS Tour?

  • » Cutting-Edge Technologies
  • » Agile Practices
  • » Peer Exchange

Current Topics:

  • Languages on the JVM: Scala, Groovy, Clojure
  • Enterprise Java
  • Core Java, Java 8
  • Agility
  • Testing: Geb, Spock, Easyb
  • REST
  • NoSQL: MongoDB, Cassandra
  • Hadoop
  • Spring 4
  • Cloud
  • Automation Tools: Gradle, Git, Jenkins, Sonar
  • HTML5, CSS3, AngularJS, jQuery, Usability
  • Mobile Apps - iPhone and Android
  • More...
Learn More »