Monday, April 28, 2014

How to run Jetty in embedded mode, for a Jersey based REST application

I wanted to be able to run Jetty in embedded mode and use Jersey for a simple REST service.  There was no single tutorial that helped me through all the steps.  I came up with the following after a lot of googling and cobbling together from various sources on the net.

I started off by creating a new Maven project in Eclipse, by choosing the following archetype
GroupId : org.mortbay.jetty.archetype
ArtifactId: jetty-archetype-assembler
Version: 7.5.1.v20110908
In Eclipse, (kepler) I had to add http://repo1.maven.org/maven2/archetype-catalog.xml as Remote Archetype catalog to be able to get the correct archetype.

I then changed the pom to reflect the latest versions available.

 <properties>  
  <jdk.version>1.7</jdk.version>  
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  <jersey.version>2.7</jersey.version>  
  <jetty.version>9.2.0.M0</jetty.version>  
  </properties>  
  <dependencies>  
  <!-- Servlet API -->  
  <dependency>  
   <groupId>javax.servlet</groupId>  
   <artifactId>javax.servlet-api</artifactId>  
   <version>3.1-b06</version>  
  </dependency>  
  <!-- Jetty -->  
  <dependency>  
   <groupId>org.eclipse.jetty</groupId>  
   <artifactId>jetty-webapp</artifactId>  
   <version>${jetty.version}</version>  
  </dependency>  
  <dependency>  
   <groupId>org.mortbay.jetty</groupId>  
   <artifactId>jsp-2.1-glassfish</artifactId>  
   <version>2.1.v20100127</version>  
  </dependency>  
  <dependency>  
   <groupId>org.glassfish.jersey.core</groupId>  
   <artifactId>jersey-server</artifactId>  
   <version>${jersey.version}</version>  
  </dependency>  
  <dependency>  
   <groupId>org.glassfish.jersey.containers</groupId>  
   <artifactId>jersey-container-servlet-core</artifactId>  
   <version>${jersey.version}</version>  
  </dependency>  
  <dependency>  
   <groupId>com.fasterxml.jackson.jaxrs</groupId>  
   <artifactId>jackson-jaxrs-json-provider</artifactId>  
   <version>2.2.2</version>  
  </dependency>  
  <dependency>  
   <groupId>org.slf4j</groupId>  
   <artifactId>slf4j-log4j12</artifactId>  
   <version>1.7.7</version>  
  </dependency>  
   </dependencies>  

The slf4j-log4j12 dependency allows you to use log4j for both jetty logging as well as application logging.

Packaging the application.
 <build>  
  <finalName>standalone-svc</finalName>  
  <resources>  
   <resource>  
    <directory>src/main/resources</directory>  
   </resource>  
   <resource>  
    <directory>src/main/webapp</directory>  
   </resource>  
  </resources>  
  <plugins>  
   <plugin>  
    <groupId>org.apache.maven.plugins</groupId>  
    <artifactId>maven-compiler-plugin</artifactId>  
    <version>3.0</version>  
    <configuration>  
     <source>${jdk.version}</source>  
     <target>${jdk.version}</target>  
    </configuration>  
   </plugin>  
   <plugin>  
    <groupId>org.eclipse.jetty</groupId>  
    <artifactId>jetty-maven-plugin</artifactId>  
    <version>${jetty.version}</version>  
   </plugin>  
   <plugin>  
    <groupId>org.apache.maven.plugins</groupId>  
    <artifactId>maven-jar-plugin</artifactId>  
    <version>2.4</version>  
    <executions>  
     <execution>  
      <id>package-jar</id>  
      <phase>package</phase>  
      <goals>  
       <goal>jar</goal>  
      </goals>  
     </execution>  
    </executions>  
   </plugin>  
   <plugin>  
    <groupId>org.apache.maven.plugins</groupId>  
    <artifactId>maven-assembly-plugin</artifactId>  
    <version>2.4</version>  
    <configuration>  
     <descriptors>  
      <descriptor>src/main/assembly/assembly.xml</descriptor>  
     </descriptors>  
     <archive>  
      <manifest>  
       <mainClass>com.pramod.Main</mainClass>  
      </manifest>  
     </archive>  
    </configuration>  
    <executions>  
     <execution>  
      <phase>package</phase>  
      <goals>  
       <goal>single</goal>  
      </goals>  
     </execution>  
    </executions>  
   </plugin>  
  </plugins>  
 </build>  
The assembly.xml looks as follows;
 <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"  
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
   xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">  
  <id>standalone</id>  
  <formats>  
   <format>jar</format>  
  </formats>  
  <baseDirectory></baseDirectory>  
  <dependencySets>  
   <dependencySet>  
    <unpack>true</unpack>  
   </dependencySet>  
  </dependencySets>  
 </assembly>  
Executing mvn install, will generate jar file in the target directory, which can be executed by the command
java -jar <>.jar