Friday, December 28, 2012

Configuring Jetty, Maven, and Eclipse together


Environment Information:
JDK 1.5+
Eclipse 3.4.0
maven 2.0.10
m2eclipse 0.9.7 (maven plugin for eclipse)
Jetty 6.1.10
Spring
JPA,Hibernate
Maven Jetty Configuration:
In your maven project's pomx.xml, in your <build> section, add the jetty plugin.  An example can be found at the Jetty website here:
It is very important to keep <scanIntervalSeconds> set to zero.  This setting tells Jetty how often (in seconds) to scan the webapp for changes and if changes are found, it re-cycles the web container.  You don't want to do this and setting it to zero will disable it.
Configuring Jetty to start within Eclipse:
Next, create an easy way to launch your jetty server.  I'm using Jetty through Maven and Eclipse.  Here is how I setup an Eclipse External Tool to launch my Jetty server:
To make sure it is listening for a debugger, make sure that a MAVEN_OPTS environment variable is set with the following options:
-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=4000,server=y,suspend=n
The address parameter is what port the jetty server will be listening on for the remote debugger.
Click 'Run' and your jetty server should start running in Eclipse's console window.  It should look something like this (depending on your log4j config):
Listening for transport dt_socket at address: 4000
  [INFO] Scanning for projects...  [INFO] Searching repository for plugin with prefix: 'jetty'.  [INFO] ------------------------------------------------------------------------  [INFO] Building myProject  [INFO]    task-segment: [jetty:run]  [INFO] ------------------------------------------------------------------------  [INFO] Preparing jetty:run  2010-05-27 15:39:25.733::INFO:  Started SelectChannelConnector@0.0.0.0:8080  [INFO] Started Jetty Server
Attaching the Debugger:
Next, setup a debugger.  In Eclipse open 'Debug Configurations', and create a new 'Remote Java Debugger'.   Select your eclipse project, set the host to localhost, and set the port to 4000, or whatever you defined earlier.
Press the Debug Button and the remote debugger should attach to your jetty server.
Finally, make sure Build Automatically is enabled in eclipse (Project->Build Automatically).
At this point your environment is enabled for debugging code on your Jetty server through Eclipse.  Breakpoints, watch variables, you name it.
Hot deploy is also enabled.  If you modify some java code, the automatic builder should compile the .java file to a .class file.  The remote debugger will see it and use it, all without restarting your jetty server or its web container.  This will not work for things like adding static variables, new domain classes, or new injectable service methods that require the application to acknowledge them on startup.

No comments:

Post a Comment