jetty

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 ‘App.java’ 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.

                    <properties>
                        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
                        <jettyVersion>9.2.6.v20141205</jettyVersion>
                    </properties>
                    <build>
                       <plugins>
                           <plugin>
                               <groupId>org.eclipse.jetty</groupId>
                               <artifactId>jetty-maven-plugin</artifactId>
                               <version>${jettyVersion}</version>
                           </plugin>
                       </plugins>
                   </build>
    
  • 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 AppTest.java from this folder.
  • Renaming Class name. In this step we will rename source file in src\main\java\javacodegeeks from App.java to MavenJettyInt.java. 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 java.io.IOException;
    
    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{
    
    	@Override
    	protected void doGet(HttpServletRequest request,
    			HttpServletResponse response) throws ServletException, IOException {
    		response.setContentType("text/html");
    		response.setStatus(HttpServletResponse.SC_OK);
    		response.getWriter().println("
    
    

    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"?>
                <web-app
                   xmlns="http://xmlns.jcp.org/xml/ns/javaee"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
                   metadata-complete="false"
                   version="3.1">
    
                  <servlet>
                    <servlet-name>jcg</servlet-name>
                    <servlet-class>com.javacodegeeks.MavenJettyInt</servlet-class>
                  </servlet>
                  <servlet-mapping>
                    <servlet-name>jcg</servlet-name>
                    <url-pattern>/jcg/*</url-pattern>
                  </servlet-mapping>
    
                </web-app>
    
  • 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="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.javacodegeeks</groupId>
                  <artifactId>MvnJettyInt</artifactId>
                  <packaging>jar</packaging>
                  <version>1.0-SNAPSHOT</version>
                  <name>MvnJettyInt</name>
                  <url>http://maven.apache.org</url>
                  <dependencies>
                        <dependency>
                            <groupId>javax.servlet</groupId>
                            <artifactId>javax.servlet-api</artifactId>
                            <version>3.1.0</version>
                            <scope>provided</scope>
                        </dependency>
                  </dependencies>
    
                  <properties>
                        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
                        <jettyVersion>9.2.6.v20141205</jettyVersion>
                  </properties>
                  <build>
                        <plugins>
                            <plugin>
                                <groupId>org.eclipse.jetty</groupId>
                                <artifactId>jetty-maven-plugin</artifactId>
                                <version>${jettyVersion}</version>
                            </plugin>
                        </plugins>
                  </build>
                  </project>
    
    
  • 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

Download
You can download the full source code of this example here: MvnJettyInt.zip

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.
Subscribe
Notify of
guest

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

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button