36

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.

JARC
  • 5,288
  • 8
  • 38
  • 43

9 Answers9

50

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
Alex
  • 8,093
  • 6
  • 49
  • 79
  • 6
    Nice! Me too. I'm on windows so I used `set GRADLE_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,address=9999,server=y,suspend=n` – Ryan Shillington Mar 27 '12 at 04:48
  • +1 Thanks this finally worked on windows. I had my statement in quotes which seemed to cause me all my trouble. – JARC Apr 04 '12 at 15:08
  • any IDE worth its salt will allow you to add breakpoints and allows to run remote sessions based on the port. Check https://www.jetbrains.com/idea/help/run-debug-configuration-remote.html – Luis Ramirez-Monterosa Oct 26 '15 at 16:10
18

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.

akhikhl
  • 2,552
  • 18
  • 23
  • I had tried Gretty, idea is very nice, but it did not support multi jetty/tomcat instance witch is very important for multi project – mahengyang Jan 17 '15 at 01:14
  • do you mean "multiple web-apps on the same servlet container"? – akhikhl Jan 17 '15 at 08:33
  • no, two independent project, and when I start the first by run `gradle run`,then turned to second project directory,run `gradle run`,gretty gives me already has an instance error – mahengyang Jan 17 '15 at 09:09
  • 1
    Please try defining distinct sets of ports for both: ```gretty { httpPort = 8081 servicePort = 8082 statusPort = 8083 } ``` – akhikhl Jan 17 '15 at 16:14
  • Gradle Distribution: Gradle wrapper from target build Gradle Version: 3.0 Java Home: C:\Java\jdk1.7.0_45 JVM Arguments: None Program Arguments: None Gradle Tasks: jettyRunDebug '' :prepareInplaceWebAppFolder UP-TO-DATE :createInplaceWebAppFolder UP-TO-DATE :processResources UP-TO-DATE :classes :prepareInplaceWebAppClasses :prepareInplaceWebApp :jettyRunDebug Listening for transport dt_socket at address: 5005 It is getting stuck here forever not able to debug the code. OS: Windows GRADLE_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,address=9999,server=y,suspend=n – Jaini Naveen Sep 21 '16 at 14:55
  • You have a pretty page. But no variant given on it works. Not a single one! Really, I do not understand why do you waste the time of us and of yours. – Gangnus Jan 13 '17 at 15:37
6

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.

thoredge
  • 12,237
  • 1
  • 40
  • 55
  • It runs in process currently I think, I'll follow your advice to make sure. Cheers – JARC Feb 21 '12 at 17:41
  • Jetty runs in process by default it seems. – JARC Feb 22 '12 at 11:06
  • What do you mean by "in process"? Do you mean the gradle process? If you're running 'gradle jettyRun' without daemon then the gradle process will be hanging until you force it to stop. – thoredge Feb 22 '12 at 12:10
  • Setting `org.gradle.jvmargs` in gradle.properties isn't working in daemon mode, either. Maybe I'm doing it wrong. Any ideas? I submitted an additional question if you want to answer there: http://stackoverflow.com/questions/18729998/debugging-jetty-daemon-process-in-gradle – Depressio Sep 10 '13 at 22:54
  • Just tried with Gradle 3.5.1 in cygwin, and found that Gradle forked a process for jetty because of the jvm args. Without --no-daemon, both processes tried to use the same port, and the non-jetty process won. With --no-daemon, the first process used the port 9999 specified in the command line, and the forked process used 5005, gradle's default. So I was able to attach to the forked jetty process using port 5005. – user9712582 Oct 10 '19 at 00:10
5

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!

user770119
  • 422
  • 1
  • 5
  • 11
3

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...

2

add this into the build.gradle

jettyRun {
    jvmArgs '-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005'
}
Damith Ganegoda
  • 4,100
  • 6
  • 37
  • 46
0

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.

gongmingqm10
  • 27
  • 1
  • 7
0

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

Sorokin Andrey
  • 419
  • 5
  • 12
0

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.

Cyrusmith
  • 747
  • 1
  • 9
  • 17