How to debug Play 2 Application built with Gradle

You're setting the debug argument in the wrong place. Setting it in the GRADLE_OPTS defines a system property to use when you're running Gradle. Because Gradle starts a new JVM process to execute the Play Framework application, you must carry these properties to the JVM runnning the application.

You can use the PlayRun task to add the JVM arguments. Something like this:

tasks.withType(PlayRun) {
    forkOptions.jvmArgs = ['-Xdebug', 
                          '-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=9999']
}

But notice that this setting will set the debug mode every time you execute the application. Maybe you should add some argument to validate if you want the debug mode.

After that, you can normally set the remote debug in your IDE.

Hope it helps ;)


Play 2.7, Java 8, Gradle combo required the following: run it with: gradle runPlay -Ddebug=true

play {
    injectedRoutesGenerator = true
    platform {
        playVersion = playV
        scalaVersion = scalaV
        javaVersion = javaV
    }
    if (System.getProperty("debug")) {
        def runPlayTask = tasks.findByName('runPlay')
        runPlayTask.forkOptions.jvmArgs = ['-Xdebug', '-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005']
    }
}```

Building on joao's answer, you can create a new task and use your remote configuration to connect to it:

task debugPlayBinary {
    doLast {
        def runPlayTask = tasks.findByName('runPlayBinary')
        runPlayTask.forkOptions.jvmArgs = ['-Xdebug', '-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005']
        runPlayTask.run()
    }
}

This allows runPlayBinary to remain untouched.