Add Jetty Maven Plugin to Your JAX-RS Project

When building RESTful Web Services for your Mobile app with Java JAX-RS and Jersey you can use any Java Servlet container to deploy and run your final .WAR file. But if you use Jetty then there is a very quick way to build and run your application using Maven and jetty-maven-plugin.

Below is a short example on how to add jetty-maven-plugin to your project, how to start up jetty and how to access your Web Service Endpoint url .

  1. Grab the jetty-maven-plugin from https://mvnrepository.com
<!-- https://mvnrepository.com/artifact/org.mortbay.jetty/jetty-maven-plugin -->
<dependency>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>8.1.16.v20140903</version>
    <scope>test</scope>
</dependency>
  1. Add the jetty-maven-plugin into your pom.xml as one more plugin. For example, if jetty-maven-plugin is the only plugin you have, then your pom.xml <build> section will look like this:
<build>
        <finalName>CodeExamplesWebApp</finalName>
        <plugins>
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>8.1.16.v20140903</version>
            </plugin>
        </plugins>
    </build>

Here is a complete pom.xml of one of my sample projects with jetty-maven-plugin added:

<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>com.appsdeveloperblog.ws</groupId>
    <artifactId>CodeExamplesWebApp</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>CodeExamplesWebApp Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.bundles/jaxrs-ri -->
        <dependency>
            <groupId>org.glassfish.jersey.bundles</groupId>
            <artifactId>jaxrs-ri</artifactId>
            <version>2.25</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.media/jersey-media-moxy -->
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
            <version>2.25</version>
        </dependency>
        
        <!-- https://mvnrepository.com/artifact/javax.ws.rs/javax.ws.rs-api -->
        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>javax.ws.rs-api</artifactId>
            <version>2.0.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.mortbay.jetty/jetty-maven-plugin -->

    </dependencies>
    <build>
        <finalName>CodeExamplesWebApp</finalName>
        <plugins>
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>8.1.16.v20140903</version>
            </plugin>
        </plugins>
    </build>
</project>

And this is it. To build your project you can use: 

mvn install

To run your project with Jetty use:

mvn jetty:run

By default Jetty will start on port 8080 will be available at:

http://localhost:8080/

If you have created your RESTful Web Services application following my example here: Create Jersey JAX-RS Project with Maven then you should be able to access your app using the following URL:

http://localhost:8080/api/users

Start Jetty on a different port

If the default port 8080 is already taken and is being used by your other Servlet Container then you can start Jetty on a different port. For example port 8888. To start Jetty on a different port simply run this Maven command:

mvn -Djetty.port=8888 jetty:run

To stop Jetty use: 

$ ctrl + c

I hope this helps. If you want to learn more about Maven and Jetty and building RESTful Web Services with Java, consider below educational materials:

Video Courses

Java Web Services Part 1. Video Course.

Java icon

Java Web Services Part 2. Video Course.

Master advanced web services concepts and implement them in easy steps Java Web Services Part 2 icon

REST Java Web Services. Video Course.

A guide to understanding, accessing, and writing a REST Java web service using Apache and Java EE. Java Web Services Part 2 icon