Wednesday, February 18, 2009

Debugging Jetty when running mvn jetty:run

It took me awhile to get this to work so hopefully this will save someone some time. If you want to debug a webapp that is run using the mvn jetty:run or mvn jetty:run-war commands then these steps will help you get going quickly.
1) setup your webapp to run using jetty: Maven Jetty Plugin Guide
2) setup the debugger in one of the following ways:
(A) setup your MAVEN_OPTS (environment variable) to include debugging by adding this "-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n".
Mine looks like this: MAVEN_OPTS='-Xms256m -Xmx512m -XX:PermSize=64m -XX:MaxPermSize=128m -agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n'
If you want it to suspend until the debugger is attached then just set suspend=y
Note that this will stop you from running more than one mvn process
(B) execute mvn using the mvndebug script (skip step 3)
mvndebug jetty:run
The only issue with this is that it suspends always and you cannot kill it without attaching to it with a debugger (this annoys me so I use the first option)
Note that you can only have one mvndebug process running at once
3) Execute mvn jetty:run
4) Attach a debugger (like the one in eclipse) to port 8000 and you are ready to debug

2 comments:

steve.mcmichael said...

Thanks for posting. I wasn't aware of mvnDebug until now. I agree about the annoyance of having to connect to the debug process and related issues with shutting down the jetty process when started this way. However, there's a simple fix: just edit mvnDebug.bat. Update the MVN_DEBUG_OPTS env variable within the script, setting '...,suspend=n,...'. Now you can run mvn with or without debug by choosing the correct launch script.

steve.mcmichael said...

Thanks for posting this. I wasn't aware of mvnDebug until now. And I agree that there are several annoyances with launching a java process under debug 'suspend=y' mode. Sometimes this is desired, but most of the time with webapps it's sufficient to launch the process with 'suspend=n'. I prefer simply editing mvnDebug. Just update '...,suspend=y,...' for the MVN_DEBUG_OPTS env variable to '...,suspend=n,...'. Then you can run Maven with or without debug mode by launching with the appropriate script.