How to run Jetty via Gradle in Debug Mode

35,175

Solution 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

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

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

Solution 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!

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

Share:
35,175
JARC
Author by

JARC

Updated on July 17, 2022

Comments

  • JARC
    JARC almost 2 years

    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
    JARC over 12 years
    It runs in process currently I think, I'll follow your advice to make sure. Cheers
  • JARC
    JARC over 12 years
    Jetty runs in process by default it seems.
  • thoredge
    thoredge over 12 years
    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.
  • Ryan Shillington
    Ryan Shillington over 12 years
    Nice! Me too. I'm on windows so I used set GRADLE_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,address=9999,server=y,suspend=‌​n
  • JARC
    JARC about 12 years
    +1 Thanks this finally worked on windows. I had my statement in quotes which seemed to cause me all my trouble.
  • Depressio
    Depressio almost 11 years
    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: stackoverflow.com/questions/18729998/…
  • mahengyang
    mahengyang over 9 years
    I had tried Gretty, idea is very nice, but it did not support multi jetty/tomcat instance witch is very important for multi project
  • akhikhl
    akhikhl over 9 years
    do you mean "multiple web-apps on the same servlet container"?
  • mahengyang
    mahengyang over 9 years
    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
  • akhikhl
    akhikhl over 9 years
    Please try defining distinct sets of ports for both: gretty { httpPort = 8081 servicePort = 8082 statusPort = 8083 }
  • Luis Ramirez-Monterosa
    Luis Ramirez-Monterosa over 8 years
    any IDE worth its salt will allow you to add breakpoints and allows to run remote sessions based on the port. Check jetbrains.com/idea/help/run-debug-configuration-remote.html
  • Jaini Naveen
    Jaini Naveen almost 8 years
    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
  • Gangnus
    Gangnus over 7 years
    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.
  • user9712582
    user9712582 over 4 years
    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.