Introduction

Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project's build, reporting and documentation from a central piece of information. It is an ideal tool to build a web application project, and such projects can use the Jetty Maven Plugin to run the web application in development mode.

You can use Maven both to build embedded Jetty applications and standards based web applications.

To understand the basic operations of building and running against Jetty, first review:

Details

Configuring Embedded Jetty with Maven

Maven uses convention over configuration, so it is best to use the project structure as Maven recommends. You can use Archetypes to quickly setup Maven projects, but for this tutorial, we will set up the structure manually:

Creating the POM Descriptor

The pom.xml file declares the project name and it's dependencies. Use an editor to create the file pom.xml with the following contents:

<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>hello-world</artifactId><version>0.1-SNAPSHOT</version><packaging>jar</packaging><name>Jetty HelloWorld</name><properties><jettyVersion>7.2.0.v20101020</jettyVersion></properties><dependencies><dependency><groupId>org.eclipse.jetty</groupId><artifactId>jetty-server</artifactId><version>${jettyVersion}</version></dependency></dependencies><build><plugins><plugin><!-- This plugin is needed for the servlet example --><groupId>org.mortbay.jetty</groupId><artifactId>jetty-maven-plugin</artifactId><version>${jettyVersion}</version></plugin><plugin><groupId>org.codehaus.mojo</groupId><artifactId>exec-maven-plugin</artifactId><version>1.1</version><executions><execution><goals><goal>java</goal></goals></execution></executions><configuration><mainClass>org.example.HelloWorld</mainClass></configuration></plugin></plugins></build></project>

Building and Running Embedded HelloWorld

You can now compile and execute the HelloWorld class by using these commands:

mvn clean compile exec:java

You can point your browser to http://localhost:8080 to see the hello world page. You can observe what Maven is doing for you behind the scenes by using the mvn dependency:tree command, which reveals the transitive dependency resolved and downloaded as:

Developing a Standard WebApp with Jetty and Maven

The example above shows how to run a hello world example as an embedded Jetty handler. The following example shows how to develop a standard webapp with Maven and Jetty. First create the Maven structure: