Skip to content

Instantly share code, notes, and snippets.

@mcroteau
Forked from naaman/gist:1053217
Created December 18, 2021 19:33
Show Gist options
  • Save mcroteau/e45ed02eed9b1ba7267801d338c8c9dd to your computer and use it in GitHub Desktop.
Save mcroteau/e45ed02eed9b1ba7267801d338c8c9dd 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.

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