Skip to content

Instantly share code, notes, and snippets.

@naaman
Created June 29, 2011 05:33
Show Gist options
  • Save naaman/1053217 to your computer and use it in GitHub Desktop.
Save naaman/1053217 to your computer and use it in GitHub Desktop.
Hot Swapping With Maven, Jetty and IntelliJ

Hot Swapping With Maven, Jetty and IntelliJ

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

Modify your jetty-maven-plugin to ignore the scan interval

  1. Open your pom and locate the plugins section

  2. In the configuration element, ensure scanIntervalSeconds is set to 0

    <plugin>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        ...
        <configuration>
            <scanIntervalSeconds>0</scanIntervalSeconds>
            ...
        </configuration>
    </plugin>
    

Create a Maven run configuration for Jetty in IntelliJ

  1. From IntelliJ, click Run > Edit Configurations
  2. Click Add New Configuration (the plus sign)
  3. Choose Maven
  4. Name it Jetty (or whatever you like)
  5. Choose the appropriate working directory
  6. In Goals, enter jetty:run
  7. Click the Runner tab
  8. 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.
  9. Click Ok

Create an Application debug configuration

  1. From IntelliJ, click Run > Edit Configurations
  2. Click Add New Configuration (the plus sign)
  3. Choose Remote
  4. Name it Jetty-Hotswap (or whatever you like)
  5. In the Port field, enter 4000. Note: If you modified the address in step 1.8 above, the port should match.
  6. Click Ok

Setup Reloading Classes After Compilation

  1. From IntelliJ, open Settings (the wrench icon)
  2. Navigate to Debugger > HotSwap
  3. Choose Always for Reload classes after compilation
  4. Click Ok

Run the Setup

  1. Run the run target, Jetty
  2. 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.

Tip: Keymap ctrl + x on Mac to Compile

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.

@bsandhu
Copy link

bsandhu commented May 29, 2013

With IntelliJ 12, its somewhat easier. You do not need to define a separate debug step.
You can create the jetty:run (named Jetty in the docs above) configuration and then launch it in debug mode with Run > Debug 'Jetty'. IntelliJ automatically adds the required JVM flags while launching and exposes the debug port.

@martin-g
Copy link

HotSwap supports just few types of changes in the code.
Check http://blogs.jetbrains.com/idea/2013/07/get-true-hot-swap-in-java-with-dcevm-and-intellij-idea/ for a better solution.

@fuyou001
Copy link

why I set scanIntervalSeconds 10,but when i change test.vm ,after 10 secnonds ,the page is not change with chrome?

@paul-lysak
Copy link

fuyou001, you need to deploy exploded WAR in order to have reload working.

@joakime
Copy link

joakime commented Sep 29, 2013

Btw,

    <groupId>org.mortbay.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>

is only for Jetty 8 and older.

For Jetty 9+ use the groupId org.eclipse.jetty

    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>

@koders
Copy link

koders commented Apr 30, 2015

This helped me a lot! Thanks!

@agseco
Copy link

agseco commented Jun 1, 2017

Apparently it doesn't work on IDEA 2016.3 CE. Did anybody get it to work on newer IDEA versions?

Many thanks.

@Timoteus78
Copy link

@agseco Did you end up figuring out how to get hot swapping to work on IDEA 2016.3 CE ?

@mikharju
Copy link

mikharju commented Feb 7, 2024

I'll comment here since I failed to find this info anywhere: Now there is a context menu option to compile and reload a class in Intellij. So while Maven target jetty:run is up in debug mode, right click in the editor window and select "Compile and reload file". It seems to detect even related changes in other files and compile those too as it some times says "compiled 3 files" or similar. Tested this in Intellij Ultimate. Not sure if it works in community edition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment