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

From Eclipsepedia

Jump to: navigation, search
m
 
(9 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
{{Jetty Tutorial
 
{{Jetty Tutorial
| Introduction = [http://maven.apache.org/ 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 | Jetty Maven Plugin]] to run the web application in development mode.
+
| introduction =  
 +
 
 +
{{Jetty Redirect|http://www.eclipse.org/jetty/documentation/current/jetty-maven-helloworld.html}}
 +
 
 +
[http://maven.apache.org/ 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 | 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.
 
You can use Maven both to build embedded Jetty applications and standards based web applications.
Line 8: Line 12:
 
* the [[Jetty/Tutorial/Embedding Jetty|Embedding Jetty Tutorial]].
 
* the [[Jetty/Tutorial/Embedding Jetty|Embedding Jetty Tutorial]].
  
 
+
| details =
== Embedded Jetty with Maven ==
+
== 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:
 
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:
Line 18: Line 22:
 
</source>
 
</source>
  
=== The HelloWorld class ===
+
=== Creating the HelloWorld Class ===
 
Use an editor to create the file <tt>src/main/java/org/example/HelloWorld.java</tt> with the following contents:
 
Use an editor to create the file <tt>src/main/java/org/example/HelloWorld.java</tt> with the following contents:
 
<source lang="java">
 
<source lang="java">
Line 68: Line 72:
 
   <packaging>jar</packaging>
 
   <packaging>jar</packaging>
 
   <name>Jetty HelloWorld</name>
 
   <name>Jetty HelloWorld</name>
 +
 +
  <properties>
 +
    <jettyVersion>7.2.0.v20101020</jettyVersion>
 +
  </properties>
  
 
   <dependencies>
 
   <dependencies>
Line 73: Line 81:
 
       <groupId>org.eclipse.jetty</groupId>
 
       <groupId>org.eclipse.jetty</groupId>
 
       <artifactId>jetty-server</artifactId>
 
       <artifactId>jetty-server</artifactId>
       <version>7.0.1.v20091125</version>
+
       <version>${jettyVersion}</version>
 
     </dependency>
 
     </dependency>
 
   </dependencies>
 
   </dependencies>
Line 79: Line 87:
 
   <build>
 
   <build>
 
     <plugins>
 
     <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>
 
       <plugin>
 
         <groupId>org.codehaus.mojo</groupId>
 
         <groupId>org.codehaus.mojo</groupId>
Line 100: Line 114:
 
mvn clean compile exec:java
 
mvn clean compile exec:java
 
</source>
 
</source>
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 <tt>mvn dependency:tree</tt> command, which reveals the transitive dependency resolved and downloaded as:
+
You can point your browser to <nowiki>http://localhost:8080</nowiki> to see the hello world page.  You can observe what Maven is doing for you behind the scenes by using the <tt>mvn dependency:tree</tt> command, which reveals the transitive dependency resolved and downloaded as:
 
<source lang="text">
 
<source lang="text">
 
> mvn dependency:tree
 
> mvn dependency:tree
Line 128: Line 142:
  
  
== Creating a Standard Web app with Jetty and Maven ==
+
== 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 web app with Maven and Jetty.  First create the Maven structure:
+
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:
  
 
<source lang="bash">
 
<source lang="bash">
 
mkdir JettyMavenHelloWarApp
 
mkdir JettyMavenHelloWarApp
cd JettyMavenHelloWebApp
+
cd JettyMavenHelloWarApp
 
mkdir -p src/main/java/org/example
 
mkdir -p src/main/java/org/example
 
mkdir -p src/main/webapp/WEB-INF
 
mkdir -p src/main/webapp/WEB-INF
Line 189: Line 203:
  
 
=== Building and Running the Web Application ===
 
=== Building and Running the Web Application ===
Now build and run the web application by using the command:
+
Copy the <tt>pom.xml</tt> and then you can build and run the web application by using the command:
 
<source lang="bash">
 
<source lang="bash">
 
mvn jetty:run
 
mvn jetty:run
 
</source>
 
</source>
You can see the static and dynamic content at http://localhost:8080.
+
You can see the static and dynamic content at <nowiki>http://localhost:8080/hello-world/hello</nowiki>.  The context path portion of the url ("hello-world") comes from the artifact ID in the pom.xml file.
  
 
=== Building a WAR file ===
 
=== Building a WAR file ===

Latest revision as of 13:54, 23 April 2013



Contents

[edit] 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:

[edit] 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:

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

Creating 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();
    }
}

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:

<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>
 
  <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:

> mvn dependency:tree
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'dependency'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Jetty HelloWorld
[INFO]    task-segment: [dependency:tree]
[INFO] ------------------------------------------------------------------------
[INFO] [dependency:tree {execution: default-cli}]
[INFO] org.example:hello-world:jar:0.1-SNAPSHOT
[INFO] \- org.eclipse.jetty:jetty-server:jar:7.0.1.v20091125:compile
[INFO]    +- javax.servlet:servlet-api:jar:2.5:compile
[INFO]    +- org.eclipse.jetty:jetty-continuation:jar:7.0.1.v20091125:compile
[INFO]    \- org.eclipse.jetty:jetty-http:jar:7.0.1.v20091125:compile
[INFO]       \- org.eclipse.jetty:jetty-io:jar:7.0.1.v20091125:compile
[INFO]          \- org.eclipse.jetty:jetty-util:jar:7.0.1.v20091125:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4 seconds
[INFO] Finished at: Tue Feb 16 16:19:08 EST 2010
[INFO] Final Memory: 11M/68M
[INFO] ------------------------------------------------------------------------


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:

mkdir JettyMavenHelloWarApp
cd JettyMavenHelloWarApp
mkdir -p src/main/java/org/example
mkdir -p src/main/webapp/WEB-INF

Creating Static Content

A web application can contain static content, so create the file src/main/webapp/index.html with the following contents:

<h1>Hello World Webapp</h1>
<a href="/hello">Hello Servlet</a>

Creating a Servlet

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

package org.example;
 
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
public class HelloServlet extends HttpServlet
{
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        response.setContentType("text/html");
        response.setStatus(HttpServletResponse.SC_OK);
        response.getWriter().println("<h1>Hello Servlet</h1>");
        response.getWriter().println("session=" + request.getSession(true).getId());
    }
}

This servlet needs to be declared in the deployment descriptor, so edit the file src/main/webapp/WEB-INF/web.xml and add the following contents:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app 
   xmlns="http://java.sun.com/xml/ns/javaee" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
   version="2.5">
  <servlet>
    <servlet-name>Hello</servlet-name>
    <servlet-class>org.example.HelloServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Hello</servlet-name>
    <url-pattern>/hello/*</url-pattern>
  </servlet-mapping>
</web-app>

Building and Running the Web Application

Copy the pom.xml and then you can build and run the web application by using the command:

mvn jetty:run

You can see the static and dynamic content at http://localhost:8080/hello-world/hello. The context path portion of the url ("hello-world") comes from the artifact ID in the pom.xml file.

Building a WAR file

You can create a Web Application Archive (WAR) file from the project with the command:

mvn package

The resulting war file is in the target directory and may be deployed on any standard servlet server or deployed to jetty.