How to run Jetty via Gradle in Debug Mode
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.
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...
JARC
Updated on July 17, 2022Comments
-
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 over 12 yearsIt runs in process currently I think, I'll follow your advice to make sure. Cheers
-
JARC over 12 yearsJetty runs in process by default it seems.
-
thoredge over 12 yearsWhat 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 over 12 yearsNice! Me too. I'm on windows so I used
set GRADLE_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,address=9999,server=y,suspend=n
-
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 almost 11 yearsSetting
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 over 9 yearsI had tried Gretty, idea is very nice, but it did not support multi jetty/tomcat instance witch is very important for multi project
-
akhikhl over 9 yearsdo you mean "multiple web-apps on the same servlet container"?
-
mahengyang over 9 yearsno, two independent project, and when I start the first by run
gradle run
,then turned to second project directory,rungradle run
,gretty gives me already has an instance error -
akhikhl over 9 yearsPlease try defining distinct sets of ports for both:
gretty { httpPort = 8081 servicePort = 8082 statusPort = 8083 }
-
Luis Ramirez-Monterosa over 8 yearsany 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 almost 8 yearsGradle 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 over 7 yearsYou 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 over 4 yearsJust 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.