Running Jetty 7 on Port 80 With Maven

I think Jetty is one of the best kept secrets in the Java world, in particular for getting a quick container up and running with full integration with Maven for local testing of Java based web applications. I was doing a non-stock configuration of Jetty 7 and Maven today which took way too much Googling to get working (mostly to custom configure the connector), so I thought I’d document the process for those looking for the same thing. If you’re looking how to get Jetty 7 working on Port 80 (particularly on Linux or OS X) with Maven, you’re in the right place. As of the time of writing, the latest stable release of Jetty looks to be 7.1.6.v20100715.

The three minor hurdles to configuring Jetty 7 and Maven are making sure port 80 is not already taken, getting the right connector implementation configured, and running Maven as sudo so that Jetty has permission to run on Port 80. In OS X, you can see if anything is already running on port 80 by running the following command from Terminal:

  1. sudo lsof -i -P | grep -i listen

If you’re on OS X and see something running on Port 80 yet you didn’t install a webserver, chances are it’s Sharing. Go to System Preferences > Sharing and disable it. Otherwise, figure out what process is running on port 80 and make it go away.

Now to configure the Jetty Maven plug-in, here is what my configuration looks like which you should replicate in your pom.xml under the plugins section (and adjust to your particular directory structure and desired context):

  1. <plugin>
  2.     <groupid>org.mortbay.jetty</groupid>
  3.     <artifactid>jetty-maven-plugin</artifactid>
  4.     <version>7.1.6.v20100715</version>
  5.     <configuration>
  6.         <connectors>
  7.             <connector implementation=“org.eclipse.jetty.server.nio.SelectChannelConnector”>
  8.                 <port>80</port>
  9.             </connector>
  10.         </connectors>
  11.         <webappsourcedirectory>${basedir}/resources/war</webappsourcedirectory>
  12.         <scanintervalseconds>10</scanintervalseconds>
  13.         <webappconfig>
  14.             <contextpath>/</contextpath>
  15.         </webappconfig>
  16.     </configuration>
  17. </plugin>

One of the difficulties I encountered was getting the connector working correctly. Since Maven has moved to be an Eclipse project, the package structure for the connector has changed and thus the reason most of the existing documentation on getting custom connectors to work did not work for me.

The last remaining hurdle to overcome is that running a standard “mvn jetty:run” doesn’t work on OS X (and likely Linux as well). You’ll see the error message “Embedded error: Permission denied”. If you run Maven with error stacktraces enabled, you’ll see an exception that looks like this:

  1. [INFO] Jetty server exiting.
  2. [INFO] ————————————————————————
  4. [INFO] ————————————————————————
  5. [INFO] Failure
  7. Embedded error: Permission denied
  8. [INFO] ————————————————————————
  9. [INFO] Trace
  10. org.apache.maven.lifecycle.LifecycleExecutionException: Failure
  11.     at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(
  12.     at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeStandaloneGoal(
  13.     at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(
  14.     at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(
  15.     at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(
  16.     at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(
  17.     at org.apache.maven.DefaultMaven.doExecute(
  18.     at org.apache.maven.DefaultMaven.execute(
  19.     at org.apache.maven.cli.MavenCli.main(
  20.     at org.apache.maven.cli.compat.CompatibleMain.main(
  21.     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  22.     at sun.reflect.NativeMethodAccessorImpl.invoke(
  23.     at sun.reflect.DelegatingMethodAccessorImpl.invoke(
  24.     at java.lang.reflect.Method.invoke(
  25.     at org.codehaus.classworlds.Launcher.launchEnhanced(
  26.     at org.codehaus.classworlds.Launcher.launch(
  27.     at org.codehaus.classworlds.Launcher.mainWithExitCode(
  28.     at org.codehaus.classworlds.Launcher.main(
  29. Caused by: org.apache.maven.plugin.MojoExecutionException: Failure
  30.     at org.mortbay.jetty.plugin.AbstractJettyMojo.startJetty(
  31.     at org.mortbay.jetty.plugin.AbstractJettyMojo.execute(
  32.     at org.mortbay.jetty.plugin.JettyRunMojo.execute(
  33.     at org.apache.maven.plugin.DefaultPluginManager.executeMojo(
  34.     at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(
  35.     … 17 more
  36. Caused by: Permission denied
  37.     at Method)
  38.     at
  39.     at
  40.     at
  41.     at org.eclipse.jetty.server.nio.SelectChannelConnector.doStart(
  42.     at org.eclipse.jetty.util.component.AbstractLifeCycle.start(
  43.     at org.eclipse.jetty.server.Server.doStart(
  44.     at org.mortbay.jetty.plugin.JettyServer.doStart(
  45.     at org.eclipse.jetty.util.component.AbstractLifeCycle.start(
  46.     at org.mortbay.jetty.plugin.AbstractJettyMojo.startJetty(
  47.     … 21 more

This is because you need elevated permissions to run applications on lower ports on *nix based systems. Simply run it as sudo, “sudo mvn jetty:run” and you’ll be good to go.

Not a terribly tough problem to overcome on your own, but I thought I’d try and try to save someone some time if they run across the same issues.

This entry was posted in Culture, Languages, Operating Systems, Servers, Tips, Hacks, & Tricks, Tools and tagged , , , , , , , . Bookmark the permalink.

Leave a reply