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

Difference between revisions of "Jetty/Tutorial/Jetty and Maven HelloWorld"

Line 61: Line 61:
 
=== The POM descriptor ===
 
=== The POM descriptor ===
 
The <tt>pom.xml</tt> file declares the project name and it's dependencies. Use and editor to create the file <tt>pom.xml</tt> with the following contents:
 
The <tt>pom.xml</tt> file declares the project name and it's dependencies. Use and editor to create the file <tt>pom.xml</tt> with the following contents:
 +
<source lang="xml">
 +
<project xmlns="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>
 +
 +
  <dependencies>
 +
    <dependency>
 +
      <groupId>javax.servlet</groupId>
 +
      <artifactId>servlet-api</artifactId>
 +
      <version>2.5</version>
 +
    </dependency>
 +
    <dependency>
 +
      <groupId>org.eclipse.jetty</groupId>
 +
      <artifactId>jetty-server</artifactId>
 +
      <version>7.0.1.v20091125</version>
 +
    </dependency>
 +
  </dependencies>
 +
 +
  <build>
 +
    <plugins>
 +
      <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>
 +
</source>
  
 
== Standard Web app with Jetty and Maven ==
 
== Standard Web app with Jetty and Maven ==

Revision as of 20:06, 15 February 2010



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/Feature/Jetty Maven Plugin to run the web application in development mode.

Maven can be used both to build embedded jetty appliations and standards based webapplications.

Details

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


Embedded Jetty with Maven

Maven uses convention over configuration, so it is best to use the project structure as recommended by maven. Archetypes can be used to quickly setup maven projects, but for this tutorial, we will setup the structure manually:

mkdir JettyMavenHelloWorld
cd JettyMavenHelloWorld
mkdir -p src/main/java/org/example

The HelloWorld class

Use an editor to create the file src/main/java/org/example/HelloWorld.java with the following contents:

package org.example;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;
 
public class HelloWorld extends AbstractHandler
{
    public void handle(String target,
                       Request baseRequest,
                       HttpServletRequest request,
                       HttpServletResponse response) 
        throws IOException, ServletException
    {
        response.setContentType("text/html;charset=utf-8");
        response.setStatus(HttpServletResponse.SC_OK);
        baseRequest.setHandled(true);
        response.getWriter().println("<h1>Hello World</h1>");
    }
 
    public static void main(String[] args) throws Exception
    {
        Server server = new Server(8080);
        server.setHandler(new HelloWorld());
 
        server.start();
        server.join();
    }
}

The POM descriptor

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

<project xmlns="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>
 
  <dependencies>
    <dependency>
      <groupId>javax.servlet</groupId>
       <artifactId>servlet-api</artifactId>
       <version>2.5</version>
    </dependency>
    <dependency>
      <groupId>org.eclipse.jetty</groupId>
      <artifactId>jetty-server</artifactId>
      <version>7.0.1.v20091125</version>
    </dependency>
  </dependencies>
 
  <build>
    <plugins>
      <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>

Standard Web app with Jetty and Maven

Back to the top