Maven Jetty Plugin Example

In this example, we shall show you how to integrate Apache Maven with Jetty. Apache Maven is a software project management and comprehension tool. It provides powerful features like superior dependency management including automatic updating and transitive dependencies.

It follows the principle of convention over configuration, due to which one can start with a minimal configuration and sensible defaults will be provided for all the missing configuration. Maven uses central repositories where various artifacts like JAR files can be hosted. It comes with a mechanism that resolves all project dependencies from these central repositories. So effectively you are resolved from keeping and providing JAR files on your project’s classpath.

Maven needs a file called ‘pom.xml’ where one can define dependencies as we will be seeing in the example below. Once you choose to build the project, these dependencies will be automatically fetched from central repository and put on your application’s classpath. Jetty is a web server and a servlet container. It also provides support for SPDY, WebSocket, OSGi, JMX, JNDI, JAAS etc. So it is quite similar to the likes of other application containers. However if differs from them by providing a really tiny memory footprint and being Embeddable.

This means we do not need to install Jetty and then deploy our application. We just need to start our application from command line and Jetty will be started as an embedded container inside our application.This makes Jetty quite handy for tasks like Micro Services implementation and integration tests writing.

For this example we will be using Apache Maven 3.2.5 and Jetty Version 9.2.6. The example is compilable on Java 5 and above.

1. Creating Maven Project

Initially, we will be creating a bare minimum maven project. This will be a command line Hello World application.Later on we will be integrating Jetty in it. Once you have maven installed and running on your machine, issue following command from command line.

    mvn archetype:generate -DgroupId=com.javacodegeeks -DartifactId=MvnJettyInt -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Maven will be downloading all dependencies from the default Maven Central repository and creating a bare minimum hello world project for us. Once the above command completes, Maven would have generated a really small pom.xml file and src folder for us. There is a single class called ‘’ in the src folder.

2. Integrating Jetty

To integrating Jetty, we need to make following changes to pom.xml

  • Adding Jetty maven plugin. This is required to run jetty from maven command line.

  • Removing JUnit Test cases. As default application contains JUnit tests which we do not require for our example.We will be removing those from /MvnJettyInt/src/test/java/javacodegeeks.Remove from this folder.
  • Renaming Class name. In this step we will rename source file in src\main\java\javacodegeeks from to This is just to give our class a more meaningful name.
  • Creating Servlet. We will be creating a hello world servlet in this step. This servlet will be hosted in embedded jetty container in next step.

    package com.javacodegeeks;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
     * Hello world!
    public class MavenJettyInt extends HttpServlet{
    	protected void doGet(HttpServletRequest request,
    			HttpServletResponse response) throws ServletException, IOException {

    Hello Servlet

    "); } }
  • Adding web.xml to project. We need to create web.xml in /src/main/webapp/WEB-INF.It will contain following contents.

                <?xml version="1.0" encoding="UTF-8"?>
  • Adding servlet dependency in pom.xml. As we are using servlet now. We need to add dependency in pom.xml.The complete file is as follows

                <project xmlns="" xmlns:xsi=""
  • Building and running project. As the pom.xml file is changed, we need to rebuild the project so that maven can download all new dependencies using following command.

                cd MvnJettyInt
                mvn clean install
                mvn jetty:run

    Open localhost:8080/jcg in browser.

5. Download the source code

You can download the full source code of this example here:

Arpit Gautam

Arpit has graduated from Computer Science and Engineering Department from the Institute of Technology and Management Gurgaon. He is working in enterprise product development since a decade and worked on desktop, mobile and server side applications using java. During his studies, he participated in various coding contests and technical paper presentations. He is working as a lead Software Engineer in Workforce Management domain where he is mainly involved with projects based on Java application and C++ system programming. He is curious about writing agile code which can adapt as business changes. He likes to experiment with open source technologies and java tech stack in his spare time.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button