| <html><head> |
| <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> |
| <title>Using Maven</title><link rel="stylesheet" type="text/css" href="css/docbook.css"><meta name="generator" content="DocBook XSL-NS Stylesheets V1.76.1"><meta name="keywords" content="jetty, servlet-api, cometd, http, spdy, eclipse, maven, java, software"><link rel="home" href="index.html" title="Jetty : The Definitive Reference"><link rel="up" href="maven-and-jetty.html" title="Chapter 21. Maven and Jetty"><link rel="prev" href="maven-and-jetty.html" title="Chapter 21. Maven and Jetty"><link rel="next" href="jetty-maven-plugin.html" title="Configuring the Jetty Maven Plugin"><link xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times" rel="shortcut icon" href="images/favicon.ico"><script type="text/javascript" src="js/shCore.js"></script><script type="text/javascript" src="js/shBrushJava.js"></script><script type="text/javascript" src="js/shBrushXml.js"></script><script type="text/javascript" src="js/shBrushBash.js"></script><script type="text/javascript" src="js/shBrushJScript.js"></script><script type="text/javascript" src="js/shBrushSql.js"></script><script type="text/javascript" src="js/shBrushProperties.js"></script><script type="text/javascript" src="js/shBrushPlain.js"></script><link type="text/css" rel="stylesheet" href="css/shCore.css"><link type="text/css" rel="stylesheet" href="css/shThemeEclipse.css"><link type="text/css" rel="stylesheet" href="css/font-awesome.min.css"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><table xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times"><tr><td style="width: 25%"><a href="http://www.eclipse.org/jetty"><img src="images/jetty-header-logo.png" alt="Jetty Logo"></a><br><span style="font-size: small"> |
| Version: 9.0.6.v20130930</span></td><td style="width: 50%"><script type="text/javascript"> (function() { |
| var cx = '016459005284625897022:obd4lsai2ds'; |
| var gcse = document.createElement('script'); |
| gcse.type = 'text/javascript'; |
| gcse.async = true; |
| gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + |
| '//www.google.com/cse/cse.js?cx=' + cx; |
| var s = document.getElementsByTagName('script')[0]; |
| s.parentNode.insertBefore(gcse, s); |
| })(); |
| </script><gcse:search></gcse:search></td></tr></table><div xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Using Maven</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="maven-and-jetty.html"><i class="icon-chevron-left"></i> Previous</a> </td><th width="60%" align="center">Chapter 21. Maven and Jetty<br><a accesskey="p" href="index.html"><i class="icon-home"></i> Home</a></th><td width="20%" align="right"> <a accesskey="n" href="jetty-maven-plugin.html">Next <i class="icon-chevron-right"></i></a></td></tr></table><hr></div><div xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times" class="jetty-callout"><h5 class="callout"><a href="http://www.webtide.com/support.jsp">Contact the core Jetty developers at |
| <span class="website">www.webtide.com</span></a></h5><p> |
| private support for your internal/customer projects ... custom extensions and distributions ... versioned snapshots for indefinite support ... |
| scalability guidance for your apps and Ajax/Comet projects ... development services from 1 day to full product delivery |
| </p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="jetty-maven-helloworld"></a>Using Maven</h2></div></div></div><div class="toc"><dl><dt><span class="section"><a href="jetty-maven-helloworld.html#configuring-embedded-jetty-with-maven">Using Embedded Jetty with Maven</a></span></dt><dt><span class="section"><a href="jetty-maven-helloworld.html#developing-standard-webapp-with-jetty-and-maven">Developing a Standard WebApp with Jetty and Maven</a></span></dt></dl></div><p><a class="link" href="http://maven.apache.org/" target="_top">Apache Maven</a> 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.</p><p>It is an ideal tool to build a web application project, and such |
| projects can use the <a class="link" href="jetty-maven-plugin.html" title="Configuring the Jetty Maven Plugin">jetty-maven-plugin</a> to easily run the web |
| application and save time in development. You can also use Maven to build, |
| test and run a project which embeds Jetty.</p><p>First we'll have a look at a very simple HelloWorld java application |
| that embeds Jetty, then a simple webapp which makes use of the <a class="link" href="jetty-maven-plugin.html" title="Configuring the Jetty Maven Plugin">jetty-maven-plugin</a> to speed up the |
| development cycle.</p><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="configuring-embedded-jetty-with-maven"></a>Using Embedded Jetty with Maven</h3></div></div></div><p>To understand the basic operations of building and running against |
| Jetty, first review</p><div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem"><p><a class="link" href="advanced-embedding.html" title="Chapter 19. Embedding">embedding with |
| Jetty</a></p></li><li class="listitem"><p><a class="link" href="advanced-embedding.html#jetty-helloworld" title="Jetty Embedded HelloWorld">Jetty HelloWorld |
| example</a></p></li></ul></div><p>Maven uses convention over configuration, so it is best to use the |
| project structure Maven recommends. You can use <span class="emphasis"><em><a class="link" href=""></a><a class="link" href="http://maven.apache.org/guides/introduction/introduction-to-archetypes.html" target="_top">archetypes</a><a class="link" href=""></a></em></span> |
| to quickly setup Maven projects, but we will set up the structure manually |
| for this simple tutorial example:</p><div class="screenexample"><pre class="screen">> mkdir JettyMavenHelloWorld |
| > cd JettyMavenHelloWorld |
| > mkdir -p src/main/java/org/example</pre></div><div class="section"><div class="titlepage"><div><div><h4 class="title"><a name="creating-helloworld-class"></a>Creating the HelloWorld Class</h4></div></div></div><p>Use an editor to create the file |
| <code class="filename">src/main/java/org/example/HelloWorld.java</code> with the |
| following contents:</p><div class="informalexample"><script type="syntaxhighlighter" class="brush: java;toolbar: false"> |
| <![CDATA[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(); |
| } |
| } ]]> |
| </script></div></div><div class="section"><div class="titlepage"><div><div><h4 class="title"><a name="creating-embedded-pom-descriptor"></a>Creating the POM Descriptor</h4></div></div></div><p>The <code class="filename">pom.xml</code> file declares the project name |
| and its dependencies. Use an editor to create the file |
| <code class="filename">pom.xml</code> with the following contents:</p><div class="informalexample"><script type="syntaxhighlighter" class="brush: java;toolbar: false"> |
| <![CDATA[<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> |
| <!-- Adapt this to a version found on |
| http://central.maven.org/maven2/org/eclipse/jetty/jetty-maven-plugin/ |
| --> |
| <jettyVersion>9.0.2.v20130417</jettyVersion> |
| </properties> |
| |
| <dependencies> |
| <dependency> |
| <groupId>org.eclipse.jetty</groupId> |
| <artifactId>jetty-server</artifactId> |
| <version>${jettyVersion}</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> ]]> |
| </script></div></div><div class="section"><div class="titlepage"><div><div><h4 class="title"><a name="buildng-and-running-embedded-helloworld"></a>Building and Running Embedded HelloWorld</h4></div></div></div><p>You can now compile and execute the HelloWorld class by using |
| these commands:</p><div class="screenexample"><pre class="screen">> mvn clean compile exec:java </pre></div><p>You can point your browser to <code class="code">http://localhost:8080</code> |
| to see the hello world page. You can observe what Maven is doing for you |
| behind the scenes by using the <code class="code">mvn dependency:tree</code> command, |
| which reveals the transitive dependency resolved and downloaded |
| as:</p><div class="screenexample"><pre class="screen">> 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:9.0.0:compile |
| [INFO] +- org.eclipse.jetty:javax.servlet:jar:3.0.0.v201112011016:compile |
| [INFO] +- org.eclipse.jetty:jetty-continuation:jar:9.0.0:compile |
| [INFO] \- org.eclipse.jetty:jetty-http:jar:9.0.0:compile |
| [INFO] \- org.eclipse.jetty:jetty-io:jar:9.0.0:compile |
| [INFO] \- org.eclipse.jetty:jetty-util:jar:9.0.0:compile |
| [INFO] ------------------------------------------------------------------------ |
| [INFO] BUILD SUCCESSFUL |
| [INFO] ------------------------------------------------------------------------ |
| [INFO] Total time: 4 seconds |
| [INFO] Finished at: Thu Jan 24 16:19:08 EST 2013 |
| [INFO] Final Memory: 11M/68M |
| [INFO] ------------------------------------------------------------------------ </pre></div></div></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="developing-standard-webapp-with-jetty-and-maven"></a>Developing a Standard WebApp with Jetty and Maven</h3></div></div></div><p>The previous section demonstrated how to use Maven with an |
| application that embeds Jetty. Now we will examine instead how to develop |
| a standard webapp with Maven and Jetty. First create the Maven structure |
| (you can use the <a class="link" href="???" target="_top">maven webapp archetype</a> instead |
| if you prefer):</p><div class="screenexample"><pre class="screen">> mkdir JettyMavenHelloWarApp |
| > cd JettyMavenHelloWebApp |
| > mkdir -p src/main/java/org/example |
| > mkdir -p src/main/webapp/WEB-INF</pre></div><div class="section"><div class="titlepage"><div><div><h4 class="title"><a name="creating-servlet"></a>Creating a Servlet</h4></div></div></div><p>Use an editor to create the file |
| <code class="filename">src/main/java/org/example/HelloServlet.java</code> with |
| the following contents:</p><div class="informalexample"><script type="syntaxhighlighter" class="brush: java;toolbar: false"> |
| <![CDATA[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()); |
| } |
| }]]> |
| </script></div><p>You need to declare this servlet in the deployment descriptor, so |
| edit the file <code class="filename">src/main/webapp/WEB-INF/web.xml</code> and |
| add the following contents:</p><div class="informalexample"><script type="syntaxhighlighter" class="brush: xml;toolbar: false"> |
| <![CDATA[<?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_3_0.xsd" |
| metadata-complete="false" |
| version="3.0"> |
| |
| <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>]]> |
| </script></div></div><div class="section"><div class="titlepage"><div><div><h4 class="title"><a name="creating-plugin-pom-descriptor"></a>Creating the POM Descriptor</h4></div></div></div><p>The <code class="filename">pom.xml</code> file declares the project name |
| and its dependencies. Use an editor to create the file |
| <code class="filename">pom.xml</code> with the following contents, noting |
| particularly the declaration of the<a class="link" href="jetty-maven-plugin.html" title="Configuring the Jetty Maven Plugin"> |
| jetty-maven-plugin</a>:</p><div class="informalexample"><script type="syntaxhighlighter" class="brush: java;toolbar: false"> |
| <![CDATA[<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>war</packaging> |
| <name>Jetty HelloWorld WebApp</name> |
| |
| <properties> |
| <jettyVersion>9.0.2.v20130417</jettyVersion> /// Adapt this to a version found on http://repo.maven.apache.org/maven2/org/eclipse/jetty/jetty-maven-plugin/ |
| </properties> |
| |
| <dependencies> |
| <dependency> |
| <groupId>org.eclipse.jetty.orbit</groupId> |
| <artifactId>javax.servlet</artifactId> |
| <version>3.0.0.v201112011016</version> |
| <scope>provided</scope> |
| </dependency> |
| </dependencies> |
| |
| <build> |
| <plugins> |
| <plugin> |
| <groupId>org.eclipse.jetty</groupId> |
| <artifactId>jetty-maven-plugin</artifactId> |
| <version>${jettyVersion}</version> |
| </plugin> |
| </plugins> |
| </build> |
| |
| </project> ]]> |
| </script></div></div><div class="section"><div class="titlepage"><div><div><h4 class="title"><a name="building-and-running-web-application"></a>Building and Running the Web Application</h4></div></div></div><p>Now you can both build and run the web application without needing |
| to assemble it into a war by using the <a class="link" href="jetty-maven-plugin.html" title="Configuring the Jetty Maven Plugin">jetty-maven-plugin</a> via the |
| command:</p><div class="screenexample"><pre class="screen">> mvn jetty:run</pre></div><p>You can see the static and dynamic content at |
| <code class="code"></code>http://localhost:8080/hello</p><p>There are a great deal of configuration options available for the |
| jetty-maven-plugin to help you build and run your webapp. The full |
| reference is at <a class="link" href="jetty-maven-plugin.html" title="Configuring the Jetty Maven Plugin">Configuring the Jetty |
| Maven Plugin</a>.</p></div><div class="section"><div class="titlepage"><div><div><h4 class="title"><a name="building-war-file"></a>Building a WAR file</h4></div></div></div><p>You can create a Web Application Archive (WAR) file from the |
| project with the command:</p><div class="screenexample"><pre class="screen">> mvn package </pre></div><p>The resulting war file is in the <code class="filename">target</code> |
| directory and may be deployed on any standard servlet server, |
| including<a class="link" href="configuring-deployment.html" title="Chapter 7. Deploying to Jetty"> Jetty</a>.</p></div></div></div><script type="text/javascript"> |
| SyntaxHighlighter.all() |
| </script><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="maven-and-jetty.html"><i class="icon-chevron-left"></i> Previous</a> </td><td width="20%" align="center"><a accesskey="u" href="maven-and-jetty.html"><i class="icon-chevron-up"></i> Top</a></td><td width="40%" align="right"> <a accesskey="n" href="jetty-maven-plugin.html">Next <i class="icon-chevron-right"></i></a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 21. Maven and Jetty </td><td width="20%" align="center"><a accesskey="h" href="index.html"><i class="icon-home"></i> Home</a></td><td width="40%" align="right" valign="top"> Configuring the Jetty Maven Plugin</td></tr></table></div><p xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times"><div class="jetty-callout"> |
| See an error or something missing? |
| <span class="callout"><a href="http://github.com/jetty-project/jetty-documentation">Contribute to this documentation at |
| <span class="website"><i class="icon-github"></i> Github!</span></a></span><span style="float: right"><i>(Generated: 2013-10-30T13:20:38-05:00)</i></span></div></p><script xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times" type="text/javascript"> |
| var _gaq = _gaq || []; |
| _gaq.push(['_setAccount', 'UA-1149868-7']); |
| _gaq.push(['_trackPageview']); |
| |
| (function() { |
| var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; |
| ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; |
| var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); |
| })(); |
| </script></body></html> |