Skip to content

Instantly share code, notes, and snippets.

@brightzheng100
Created November 2, 2018 03:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brightzheng100/c0a7cb947649ea5c79631402625be69c to your computer and use it in GitHub Desktop.
Save brightzheng100/c0a7cb947649ea5c79631402625be69c to your computer and use it in GitHub Desktop.
How to Debug Gradle Projects in Eclipse

Setting up gradle debug configuration for all projects

$ touch ~/.gradle/init.gradle
$ cat > ~/.gradle/init.gradle <<EOF
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'
        }
    }
}
EOF

Import the gradle project in Eclipse as usual

Setup Remote Java Application

Open Eclipse and go to Run -> Debug Configurations.... Select the Remote Java Application in the list of configuration types on the left. Click the New toolbar button. In the Project field of the Connect tab, type or browse to select the project to use as a reference for the launch (for source lookup). In the Host field of the Connect tab, type the IP address or domain name of the host where the Java program is running. If the program is running on the same machine as the workbench, type localhost. In the Port field of the Connect tab, type the port where the remote VM is accepting connections. In our case the port will be 9009. Click Apply and then Debug.

Enable gradle Debug

For test task debugging run $ gradle -DDEBUG=true test in the terminal. For run task debugging run $ gradle -DDEBUG=true run in the terminal. Now you can use your Eclipse debugger to debug

@AndrewFerr
Copy link

An alternative to editing init.gradle is to simply pass --debug-jvm to any JavaExec or Test task, like run and test.

This sets the listening port to 5005, but it can be configured (link to docs explaining how).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment