How to run Jetty via Gradle in Debug Mode

2019-01-21 09:23发布

问题:

Does anyone know how to configure the jetty gradle plugin to run in debug mode so that I can attach a remote debugger?

I've tried setting the gradle and java opts to:

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

...but it doesn't seem to work.

I'm able to get my own jetty installation working fine, just not via gradle (jettyRun or jettyRunWar).

Regards.

回答1:

On Linux:

export GRADLE_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,address=9999,server=y,suspend=n"
gradle jettyRun

On Windows:

set GRADLE_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,address=9999,server=y,suspend=‌​n
gradle jettyRun


回答2:

Try using Gretty plugin, it provided gradle tasks jettyRunDebug, jettyStartDebug etc.

Source code and doc: https://github.com/akhikhl/gretty

Disclosure: I am author of Gretty plugin.



回答3:

Are you running gradle in daemon mode? As I understand it the daemon will then be running the jetty instance. Therefore you'll need to set the JVM args for the daemon. This should be possible by setting the org.gradle.jvmargs in gradle.properties.

See http://gradle.org/docs/current/userguide/tutorial_this_and_that.html#sec:gradle_properties_and_system_properties

Simply project that works here in non-daemon mode

build.gradle:

apply plugin: 'idea'
apply plugin: 'jetty'

src/main/java/com/Test.java:

package com;
public class Test {
    static public String greet() {
        return "Hi";
    }
}

src/main/webapp/index.jsp:

<%@ page import="com.Test" %>
<html><body>
<%= Test.greet() %>
</body></html>

Command-line (in cygwin though):

$ GRADLE_OPTS='-Xdebug -Xrunjdwp:transport=dt_socket,address=9999,server=y,suspend=n' gradle jettyRun

Gradle then hangs and I can put debugger from Intellij on port 9999 and set a breakpoint in the java file. When I then try to open the web page jetty informs me about I will hit the breakpoint.



回答4:

Mine's a multi-project gradle build and I tried:

$ export GRADLE_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,address=9999,suspend=y,server=y"
$ gradle jettyRun

And that did NOT work. I even tried adding -Xnoagent to the GRADLE_OPTS setting above but that too did not make a difference. Also, setting JAVA_OPTS instead of GRADLE_OPTS did not solve the problem either. What solved the problem for me was adding a gradle.properties with:

org.gradle.jvmargs=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,address=5005,suspend=y

And immediately I could hit the breakpoint. May be solutions mentioned in other answers did not work for me because it is a multi-project build. Not sure!

Just wanted to provide the solution that worked for me in case above solutions do not work for other folks.

P.S: Tried with gradle 1.5/1.6 and adding the setting above to gradle.properties works for both versions!



回答5:

set GRADLE_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,address=9999,server=y,suspend=n does NOT work for me too when run with gradle jettyRunWar.

I found another solution which works, run gradle jettyRunWar with below options gradle -Dorg.gradle.jvmargs="-Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n" jettyRunWar.

But when I add the same parameter in gradle.properties, it doesn't work...



回答6:

add this into the build.gradle

jettyRun {
    jvmArgs '-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005'
}


回答7:

Im my cases, it doesn't work until I run the following command. GRADLE_OPTS='-XX:MaxPermSize=256M -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=4001' gradle jettyRun

And when it works, in the server console I can use System.out.println(...) to inspect what I want to see. As for breakpoint debug, unfortunately, I haven't find a way to it. Anyone knows how, welcome to add complement.



回答8:

Also, please look at this two links from official wiki:

https://github.com/akhikhl/gretty/issues/36

http://akhikhl.github.io/gretty-doc/Debugger-support.html

It can help you to properly configurate gretty plugin to debug jetty application with IntelliJ Idea



回答9:

I ran it with org.gradle.debug property:

./gradlew -Dorg.gradle.debug=true jettyRun

At this point gradle freezes and waits for incoming debug connections.

Then I created Remote Run configuration in IntelliJ with value of "Command line arguments for running remote JVM" to be -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005

Finally, I ran this new configuration, gradle resumed progress and IDE stopped at the first breakpoint.



标签: Jetty gradle