Hello world with Jetty + Maven + Eclipse WTP tutorial

Eclipse Web Tools Project (WTP) is tooling to ease development Java EE and web applications. It provides out-of-the-box support for Tomcat servlet container. For unknown reason it misses popular Jetty container despite Eclipse Foundation project too. In this tutorial I present how to create Maven based web project in Eclipse WTP which runs on Jetty. Run-jetty-run makes all this possible in extremely easy way.

Why to move from Tomcat and classical WTP development style? Lightweight Jetty runs a number times faster then Tomcat while Maven offers IDE-neutral development model. In extreme case one engineer can use Eclipse, other commandline and TextAdept.

Described setup includes

  • Eclipse 4.2 (Juno) with m2eclipse
  • Maven 3.0.4
  • Jetty 8.1.2
  • Run Jetty Run plug-in 3.2.1

Run-jetty-run is the key piece which allows us to enter modern Maven-style Java web development with blazing fast Jetty. There is also Jetty WTP plugin but it is not actively maintained anymore and over Run-jetty-run miss many features.

Installation

m2e and m2e-wtp

Go to Help → Eclipse Marketplace. Install m2e and m2e-wtp (easiest to search for “m2e-wtp” and install it which installs also m2e).

alt text
Install m2e-wtp (not abandoned version from github!)

Accept licence, wait a minute, restart Eclipse.

run-jetty-run

Again, just search for “run-jetty-run” in Eclipse Marketplace and install this sweet plug-in. It’s all you need to do. Run-jetty-run ships with Jetty already (no lengthy download & setup of Tomcat anymore).

Create new Maven webapp project

From Eclipse

  1. Go to New → Other… or Ctrl-N.
  2. You should find Maven group in the list. Choose Maven Project. New    _030
  3. Keep defaults or customize wizard’s first page.
  4. Filter out for “weppapp” and choose maven-archetype-webapp New Maven Project _031
  5. Select some group and artifact id and Finish wizard.

From commandline

If you prefer commandline version:

mvn archetype:generate -DgroupId=com.yourcompany -DartifactId=yourprojectname -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

Afterwards you need to convert this plain Maven project to Eclipse WTP project using Maven Eclipse plugin:

$ mvn eclipse:eclipse -Dwtpversion=2.0

Notice -Dwtpversion=2.0 which creates Eclipse WTP project, not Eclipse Java project! Re-run this command to re-generate Eclipse files (.project and .classpath files, .settings folder) whenever you manually update dependencies in pom.xml or you just encounter any “depedency not found” errors.

You may see how are dependencies reflected in WTP on Deployment Assembly project properties page.
wtp-deyploment-assembly-page.png

Update web.xml to Servlet 3.0

Maven webapp creates prehistorical Servlet 2.3 web.xml, therefore make it up-to-date with Servlet 3.0:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
        id="WebApp_ID" version="3.0">
  <!-- your web.xml content here -->
</web-app>

Update compiler levels to JDK 7

Again, WTP from Juno still sets compiler level to old JDK 5. So first change it to JDK 7 on Java Build Path page of project properties.

jdk7-build-path.png

Also update project’s Java facet to prevent Eclipse “Java compiler level does not match the version of the installed Java project facet” error.

Selection_026.png

Create HelloServlet

  1. From New → Other wizard choose Servlet.
  2. On the second page name it for example HelloServlet. Create Servlet _034
  3. Next, Next, Finish.

Add javax.servlet dependency

Unfortunately Eclipse WTP again needs our help. This time JDT complains about unknown class types like HttpServletResponse etc. However clicking “Fix project setup” is just one click away and sounds promising, it will only add dependency on javax.servlet.* for Eclipse project, not for Maven.

Don't click "Fix project setup"!
Don’t click "Fix project setup"!

WTP is not Maven-aware so dependency to pom.xml must be added manually via either m2eclipse GUI or editing pom.xml.

In m2eclipse way, open pom.xml, go to Dependencies tab, click Add and add artifactId javax.servlet-api from javax.servlet groupId. Don’t forget to change scope to provided.

Select Dependency _038

This will add the following to your pom.xml:

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1-b05</version>
    <scope>provided</scope>
</dependency>

We are changing dependency scope to provided because it will tell Maven that javax.servlet JAR with is already provided (by servlet container in this case) and to do not ship it with final WAR.

Run

Via Run-jetty-run

Right click on project name, choose Run As → Run Jetty to directly launch webapp.

For more setup details, open Run Configurations from Run menu and create run configuration for your webapp under Jetty Webapp node. In particular I always change Jetty version from prehistorical 6.x to 8.x.

Run Configurations _039

From commandline

Running from commandline needs one extra step which adds jetty-maven plugin to pom.xml’s required plugins.

...
        <build>
                <finalName>webapptest</finalName>
                <plugins>
                        <plugin>
                                <!-- Jetty < 9 is in org.mortbay.jetty groupId -->
                                <groupId>org.mortbay.jetty</groupId>
                                <version>8.1.9.v20130131</version>
                                <!-- Starting from Jetty 9 groupId is org.eclipse.jetty already -->
                                <!-- <groupId>org.eclipse.jetty</groupId> -->
                                <!-- Change to latest version available from http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.eclipse.jetty%22%20AND%20a%3A%22jetty-maven-plugin%22 -->
                                <!-- <version>9.x.y.z</version> -->
                                <artifactId>jetty-maven-plugin</artifactId>
                        </plugin>
                </plugins>
        </build>
</project>

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

$ mvn jetty:run

This starts Jetty and serves up your project on http://localhost:8080/. Jetty continues to run until you stop it. While it runs, it periodically scans for changes to your project files, so if you save changes and recompile your class files, Jetty redeploys your webapp, and you can instantly test the changes you just made.

To stop Jetty press Ctrl-C in the terminal window where it is running.

For details see jetty-maven plugin wiki page.

Enable hot deploy

To enjoy hot deploy (change of source file will automatically re-deploy webapp) tick and set some non-zero period in Source Code Monitor List tab of Run Configuration.

scan-interval.png

Test web app

If everything goes well last line of Jetty in console is saying something like

2013-02-18 23:06:28.973:INFO:oejs.AbstractConnector:Started SelectChannelConnector@0.0.0.0:8080

which simply means you can open your browser and point to localhost:8080. On servlet code change Jetty redeploys webapp:

Restarting webapp ...
2013-02-18 23:13:59.166:INFO:oejsh.ContextHandler:started o.e.j.w.WebAppContext{/webapptest,[file:/home/libor/workspace/webapptest/src/main/webapp/]}
2013-02-18 23:13:59.170:INFO:oejsh.ContextHandler:started o.e.j.w.WebAppContext{/webapptest,[file:/home/libor/workspace/webapptest/src/main/webapp/]}
2013-02-18 23:13:59.170:INFO:oejsh.ContextHandler:started o.e.j.w.WebAppContext{/webapptest,[file:/home/libor/workspace/webapptest/src/main/webapp/]}
2013-02-18 23:13:59.170:INFO:oejsh.ContextHandler:started o.e.j.w.WebAppContext{/webapptest,[file:/home/libor/workspace/webapptest/src/main/webapp/]}
Restart completed.

Download final application

If you didn’t followed steps above you may review and download sample project on Bitbucket.org. Clone or download somewhere and import to Eclipse. Projects includes run configuration (a .launch) file for right click Run As → jetty-maven-eclipse-wtp-sample to launch application in Jetty.

Alternatively download version jetty-maven-eclipse-wtp-sample.zip. This file is static and will not reflect updates which will be only on Bitbucket project.

Resources

About Libor Jelinek

Enthusiastic Eclipse, OSGi and Java desktop programmer, happy Ubuntu Linux user, early adopter and open source believer. He's living in small town near to Prague (Czech Republic) and splitting free time among yoga, vegetarian cooking and dog. See more.

6 thoughts on “Hello world with Jetty + Maven + Eclipse WTP tutorial

  • Pingback: Today’s Links « /dev

  • Andrew says on April 28, 2013, 2:39 am | Reply

    Thanks, that was really useful! I got my first java web app working with all the magic eclipse and maven offer.

    Coming from perl/apache it seems overly complicated at the moment. It’ll probably make more sense later.

  • Ian says on June 23, 2013, 4:25 am | Reply

    Wasted hours on this until I found this page. Frankly who are the idiots/brainiacs creating the most &&**(* and &*&*& constructs in the galaxy? This is 2013.

    I repeat… 2013.

    I repeat again… 2013.

    Processing power and CPUs shall be used to make things simpler and better.

    Servlet 3.0 and eclipse and maven = nighmare for most people.

    Too many moving parts… too much crapping out generalissimo on a stupid detail missing… the reason so few are programmers and most of these are asocial compulsive obsessive types with a masochistic stream.

    Sadly their “creation” only make life uneasy for others who are “forced” to use their crap.

    Hopefully the rise of pure functional languages and the elimination of complexity will mean death to this.

    Meanwhile… I am glad I found this.

    • Kjetil says on March 10, 2014, 3:14 pm | Reply

      Thank you for this comment, and thanks to the author for the tutorial. The comment made me feel less stupid, after struggeling a lot with this setup, and the tutorial helped me make a breakthrough. Moving on! :)

  • web development India says on February 19, 2014, 8:02 am | Reply

    Thanks for sharing the web development tutorial here. Keep up the good work. All the best.

  • zargin says on May 5, 2014, 11:06 am | Reply

    First, thank you so very much for this tutorial!

    But, why is it that the page does not appear on me? The last line of Jetty console was the same, but when I access http://localhost:8080/, it said Error 404 not found…

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

In an effort to prevent automatic filling, you should perform a task displayed below.