Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Jetty/Feature/Jetty Maven Plugin

< Jetty‎ | Feature
Revision as of 07:15, 10 August 2009 by Athena.webtide.com (Talk | contribs)



Introduction

The Jetty Maven plugin is useful for rapid development and testing. It can be added to any webapp project which is structured according to the usual Maven defaults.

The plugin can then periodically scan your project for changes and automatically redeploy the webapp if any are found. This makes the development cycle more productive by eliminating the build and deploy steps: you use your IDE to make changes to the project, and the running web container will automatically pick them up, allowing you to test them straight away.

Feature

Quick Start

First, add jetty-maven-plugin to your pom.xml definition:

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

Then, from the same directory as your root pom.xml, simply type:

 mvn jetty:run

This will start Jetty and serve up your project on http://localhost:8080/.

Jetty will continue to run until you stop it. While it runs, it will periodically scan for changes to your project files, so if you save changes and recompile your class files, Jetty will redeploy your webapp, and you can instantly test the changes you just made.

Stopping the Plugin from the Command Line

You can terminate the plugin with a <ctrl-c> in the terminal window where it is running, or by executing the stop goal in another terminal window.

If you wish to be able to use mvn jetty:stop then you need to configure the plugin with a special port number and key that you also supply on the stop command:

<plugin>
  <groupId>org.mortbay.jetty</groupId>
  <artifactId>jetty-maven-plugin</artifactId>
  <configuration>
    <stopPort>9966</stopPort>
    <stopKey>foo</stopKey>
  </configuration>
</plugin>

Then, while Jetty is running, type:

   mvn jetty:stop

The <stopPort> must be free on the machine you are running on. If this is not the case, you will get an "Address already in use" error message after the "Started SelectedChannelConnector ..." message.

Plugin Goals

The jetty:run goal only runs on assembled webapps which follow the usual Maven defaults, as follows:

  • resources in ${basedir}/src/main/webapp
  • classes in ${project.build.outputDirectory}
  • web.xml in ${basedir}/src/main/webapp/WEB-INF/

If, for whatever reason, you cannot run on an unassembled webapp, the plugin also supports the jetty:run-war and jetty:run-exploded goal, which work on unassembled webapps. These are discussed further below.


Using mvn jetty:run for Unit Testing

One useful feature of the plugin is its automatic plugin execution. You can automatically have your webapp started at the beginning of the tests, and stopped at the end rather than manually executing mvn jetty:run on the command line.

To do this, you need to set up a couple of <execution> scenarios for the Jetty plugin and use the <daemon>true</daemon> configuration option to force Jetty to only execute while Maven is running, instead of running indefinitely.

The pre-integration-test and post-integration-test Maven build phases can be used to trigger the execution and termination of Jetty:

<plugin>
  <groupId>org.mortbay.jetty</groupId>
  <artifactId>jetty-maven-plugin</artifactId>
  <configuration>
    <scanIntervalSeconds>10</scanIntervalSeconds>
    <stopKey>foo</stopKey>
    <stopPort>9999</stopPort>
  </configuration>
  <executions>
    <execution>
      <id>start-jetty</id>
      <phase>pre-integration-test</phase>
      <goals>
        <goal>run</goal>
      </goals>
      <configuration>
       <scanIntervalSeconds>0</scanIntervalSeconds>
       <daemon>true</daemon>
      </configuration>
    </execution>
    <execution>
      <id>stop-jetty</id>
      <phase>post-integration-test</phase>
      <goals>
        <goal>stop</goal>
      </goals>
    </execution>
  </executions>
</plugin>
Note.png

Back to the top