Based on Configuring Jetty, Maven, and Eclipse together with Hot Swap
I've always been a bit jealous when it comes to the Play! framework and the great dev mode they have for hot swapping classes at runtime. Jetty has a configuration setting, scanIntervalSeconds, that mimics this when working with a more traditional WAR, but does so by looking for changes to a file and restarting the server.
Fortunately, Jetty also provides the ability to rapidly test code with hot swapping. No more server restarts. The trick to getting hot swapping to work is to attach a remote debugger to your Jetty process. The following instructions outline how to do this in IntelliJ (tested with IDEA 10.5 CE).
-
Open your pom and locate the plugins section
-
In the configuration element, ensure
scanIntervalSeconds
is set to0
<plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> ... <configuration> <scanIntervalSeconds>0</scanIntervalSeconds> ... </configuration> </plugin>
- From IntelliJ, click
Run
>Edit Configurations
- Click
Add New Configuration
(the plus sign) - Choose
Maven
- Name it
Jetty
(or whatever you like) - Choose the appropriate working directory
- In Goals, enter
jetty:run
- Click the
Runner
tab - In VM Parameters, enter
-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=4000
. Note: the address here corresponds to the port you will debug with. Using 4000 leaves the more standard 5005 debug port open for other debuggers. - Click
Ok
- From IntelliJ, click
Run
>Edit Configurations
- Click
Add New Configuration
(the plus sign) - Choose
Remote
- Name it
Jetty-Hotswap
(or whatever you like) - In the
Port
field, enter4000
. Note: If you modified the address in step 1.8 above, the port should match. - Click
Ok
- From IntelliJ, open
Settings
(the wrench icon) - Navigate to
Debugger
>HotSwap
- Choose
Always
forReload classes after compilation
- Click
Ok
- Run the run target,
Jetty
- Run the debug target,
Jetty-Hotswap
The setup for hot swapping is done. All you need to do is start changing some code -- no server restarts. The annoyance with IntelliJ (as opposed to Eclipse's autobuild feature) is that it won't compile classes on save. You will have to compile them yourself.
Build
> Compile
'...filename.java' will compile your class. The keymap on Mac is shift
+ command
+ f9
by default -- ugly, a four key combo on a laptop. To further simplify, I added the keymap ctrl
+ x
to the compile menu option. This doesn't conflict with any of the default key mappings on OS X and is just as fast as saving the file. The keymap may vary from OS to OS, or based on your existing keymaps, so you may have to find a keymap that works best for you.