Maven – Run web project in Jetty

Jetty has a slogan, “Don’t deploy your application in Jetty, deploy Jetty in your application.” and that is really amazing thing specially when we are in development phase. It makes developer’s life more easy. In this post I will explain you how to run jetty web server in your web application. I will create a sample web application and then I will run that application in jetty by embedding jetty in application. I will use maven-jetty-plugin to demonsrate this.

Sample web application

Create one directory in your file system. In my case I have created directory with name “sample-web-app” in my file system.

Create pom.xml under root directory (i.e. under sample-web-app directory) as described below.

<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.elitejavacoder</groupId>
	<artifactId>sample-web-app</artifactId>
	<packaging>war</packaging>
	<version>1.0-SNAPSHOT</version>
	<name>Sample Maven Web App</name>
	<url>http://maven.apache.org</url>

	<build>
		<finalName>sample-web-app</finalName>
		<plugins>
			<plugin>
				<groupId>org.mortbay.jetty</groupId>
				<artifactId>maven-jetty-plugin</artifactId>
				<version>6.1.15</version>

				<configuration>
					<contextPath>/</contextPath>
					<scanIntervalSeconds>2</scanIntervalSeconds>
					<webAppSourceDirectory>${basedir}/target/sample-web-app</webAppSourceDirectory>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

Create web.xml file under $sample-web-appsrcmainwebappWEB-INF as described below.


Jetty test web application

Create sample index.jsp file under $sample-web-appsrcmainwebapp directory as below.

<html>
	<body>
		<h2>Hello World - This page is served by Jetty!</h2>
	</body>
</html>

Run following maven command from your root directory (i.e. sample-web-app directory) to build your application.

mvn clean install

Run web application with Jetty

Now run following maven command from your root directory (i.e. sample-web-app directory) to start jetty with your application.

mvn jetty:run

Now access “http://127.0.0.1:8080/index.jsp” url from your browser which will display content of index.jsp page we have created as below.

Hot deployment with Jetty

Now update sample index.jsp file which we have created under $sample-web-appsrcmainwebapp directory as below.

<html>
	<body>
		<h2>Hello World - This page is served by Jetty!</h2>
		<h2>Testing hot deployment</h2>
	</body>
</html>

Now access “http://127.0.0.1:8080/index.jsp” url from your browser which will display updated content of index.jsp page as below without restarting the server.

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=""> <s> <strike> <strong>