Saturday, November 17, 2007

Debugging a web application with IntelliJ IDEA, Maven and the jetty plugin

Hi,

Today I was doing some development in the most recent build of IntelliJ IDEA (found here), and toying with it I have found a quick way to debug any web application which is built using Maven.
It is a must in my developments to use the jetty maven plugin, so I can just start the application using:

mvn jetty:run

And then edit my pages and stuff without having to restart, or deploy to external web containers. To do that you just need to declare the jetty plugin in your POM file:

<plugin>
  <groupId>org.mortbay.jetty</groupId>
  <artifactId>maven-jetty-plugin</artifactId>
  <version>6.1.2</version>
  <configuration>
    <connectors>
    <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
        <port>8080</port>
      </connector>
    </connectors>
    <scanIntervalSeconds>10</scanIntervalSeconds>
  </configuration>
</plugin>

Open you project with IntelliJ IDEA (it is nice that now you can just open a maven POM as a project!), and then you can use this plugin to start the web application and start debugging right away. To do so:

  1. Create a new Run/Debug configuration (click on the drop down list on the left of the green arrow icon) and "Edit configurations".
  2. Click on the '+' (plus) button and create a new "Maven" configuration.
  3. Give it a name.
  4. In the goals section just put "jetty:run"
  5. And you are good to go!

Set some breakpoints and you can debug the web application now just clicking on the "debug" button.

Enjoy!