Maven Jetty Plugin Examples

jetty-big-logo

Few Maven Jetty 8.x and 9.x plugin examples, just for quick reference.

1. Maven Jetty Plugin 9.x

Note
You need to use Maven 3 and Java 1.7 for Maven Jetty 9.x plugin.

1.1 The ‘groupId’ is org.eclipse.jetty, by default, it runs on port 8080, in root context ‘/’.

pom.xml

  <plugin>
	<groupId>org.eclipse.jetty</groupId>
	<artifactId>jetty-maven-plugin</artifactId>
	<version>9.2.11.v20150529</version>
  </plugin>

To run the Maven Jetty plugin

terminal

$ mvn jetty:run

Deployed URL : http://localhost:8080/

To stop the running Jetty instance

terminal

$ ctrl + c

1.2 Change a context path.

pom.xml

  <plugin>
	<groupId>org.eclipse.jetty</groupId>
	<artifactId>jetty-maven-plugin</artifactId>
	<version>9.2.11.v20150529</version>
	<configuration>
		<scanIntervalSeconds>10</scanIntervalSeconds>
		<webApp>
		     <contextPath>/abc</contextPath>
		</webApp>	 
	</configuration>
  </plugin>

Deployed URL : http://localhost:8080/abc

1.3 Change a different port.

pom.xml

  <plugin>
	<groupId>org.eclipse.jetty</groupId>
	<artifactId>jetty-maven-plugin</artifactId>
	<version>9.2.11.v20150529</version>
	<configuration>
		<scanIntervalSeconds>10</scanIntervalSeconds>
		<webApp>
		     <contextPath>/abc</contextPath>
		</webApp>
		<httpConnector>
          	     <port>8888</port>
        	</httpConnector>
	</configuration>
  </plugin>

Deployed URL : http://localhost:8888/abc

Or you can pass a system property jetty.port manually :


mvn -Djetty.port=8888 jetty:run

1.4 jetty.xml example.

pom.xml

	<plugin>
	 	<groupId>org.eclipse.jetty</groupId>
		<artifactId>jetty-maven-plugin</artifactId>
		<version>9.2.11.v20150529</version>
		<configuration>
			<scanIntervalSeconds>10</scanIntervalSeconds>
			<webApp>
			      <contextPath>/abc</contextPath>
			 </webApp>
			 <jettyXml>jetty.xml</jettyXml>
		</configuration>
	</plugin>
jetty.xml

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd">
<Configure id="Server" class="org.eclipse.jetty.server.Server">
  <Call name="addConnector">
    <Arg>
      <New id="httpConnector" class="org.eclipse.jetty.server.ServerConnector">
        <Arg name="server"><Ref refid="Server" /></Arg>
        <Set name="host"><Property name="jetty.http.host" deprecated="jetty.host" /></Set>
        <Set name="port"><Property name="jetty.http.port" deprecated="jetty.port" default="1234" /></Set>
        <Set name="idleTimeout"><Property name="jetty.http.idleTimeout" deprecated="http.timeout" default="30000"/></Set>
      </New>
    </Arg>
  </Call>
</Configure>

Deployed URL : http://localhost:1234/abc

2. Maven Jetty Plugin 8.x

2.1 The ‘groupId’ is org.mortbay.jetty, by default, it runs on port 8080, in root context ‘/’.

pom.xml

  <plugin>
	<groupId>org.mortbay.jetty</groupId>
	<artifactId>jetty-maven-plugin</artifactId>
	<version>8.1.16.v20140903</version>
  </plugin>

2.2 Change a different context path, set seconds to check for changes and automatically hot redeploy.

pom.xml

  <plugin>
	<groupId>org.mortbay.jetty</groupId>
	<artifactId>jetty-maven-plugin</artifactId>
	<version>8.1.16.v20140903</version>
	<configuration>
		<scanIntervalSeconds>10</scanIntervalSeconds>
		<webApp>
			<contextPath>/abc</contextPath>
		</webApp>
	</configuration>
  </plugin>

Deployed URL : http://localhost:8080/abc

2.3 Change a different port to start.

pom.xml

  <plugin>
	<groupId>org.mortbay.jetty</groupId>
	<artifactId>jetty-maven-plugin</artifactId>
	<version>8.1.16.v20140903</version>	
	<configuration>
		<scanIntervalSeconds>10</scanIntervalSeconds>
		<webApp>
			<contextPath>/abc</contextPath>
		</webApp>
		<connectors>
			<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
			<port>8888</port>
			</connector>
		</connectors>
	</configuration>
  </plugin>

Deployed URL : http://localhost:8888/abc

Alternatively, you can pass a system property jetty.port manually.


mvn -Djetty.port=8888 jetty:run

P.S The class SelectChannelConnector is the default Jetty connector.

References

  1. Wikipedia : Jetty Web Server
  2. Jetty Maven Plugin 9.x
  3. Jetty Maven Plugin 8.x

mkyong

Founder of Mkyong.com, passionate Java and open-source technologies. If you enjoy my tutorials, consider making a donation to these charities.

6 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Shivam Sinha
6 years ago

2. Maven Jetty Plugin 8.x
defining an external port didn’t work

me me
6 years ago

very helpful. thnks

Anthony Piñero
9 years ago

Very good…!

Bhawani
9 years ago

Hi,

After following the mentioned instruction, I am able to start the server but unable to load the index page. Browser spinner is revolving without any output.

Here is the console output.
papu@PAPU-PC /C/Users/papu/projects/simple-webapp
$ mvn jetty:run
[INFO] Scanning for projects…
[INFO]
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
[INFO]
[INFO] ————————————————————————
[INFO] Building simple-webapp Maven Webapp 1.0
[INFO] ————————————————————————
[INFO]
[INFO] >>> jetty-maven-plugin:9.2.11.v20150529:run (default-cli) @ simple-webapp >>>
[INFO]
[INFO] — maven-resources-plugin:2.6:resources (default-resources) @ simple-webapp —
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] — maven-compiler-plugin:3.5.1:compile (default-compile) @ simple-webapp —
[INFO] No sources to compile
[INFO]
[INFO] — maven-resources-plugin:2.6:testResources (default-testResources) @ simple-webapp —
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory c:Userspapuprojectssimple-webappsrctestresources
[INFO]
[INFO] — maven-compiler-plugin:3.5.1:testCompile (default-testCompile) @ simple-webapp —
[INFO] No sources to compile
[INFO]
[INFO] <<< jetty-maven-plugin:9.2.11.v20150529:run (default-cli) @ simple-webapp <<<
[INFO]
[INFO] — jetty-maven-plugin:9.2.11.v20150529:run (default-cli) @ simple-webapp —
2016-09-17 10:09:07.613:INFO::main: Logging initialized @2979ms
[INFO] Configuring Jetty for project: simple-webapp Maven Webapp
[INFO] webAppSourceDirectory not set. Trying srcmainwebapp
[INFO] Reload Mechanic: automatic
[INFO] Classes = C:Userspapuprojectssimple-webapptargetclasses
[INFO] Context path = /abc
[INFO] Tmp directory = C:Userspapuprojectssimple-webapptargettmp
[INFO] Web defaults = org/eclipse/jetty/webapp/webdefault.xml
[INFO] Web overrides = none
[INFO] web.xml file = file:/C:/Users/papu/projects/simple-webapp/src/main/webapp/WEB-INF/web.xml
[INFO] Webapp directory = C:Userspapuprojectssimple-webappsrcmainwebapp
2016-09-17 10:09:07.753:INFO:oejs.Server:main: jetty-9.2.11.v20150529
2016-09-17 10:09:08.638:INFO:oejsh.ContextHandler:main: Started o.e.j.m.p.JettyWebAppContext@261d8190{/abc,file:/C:/Users/papu/projects/simple-webapp
src/main/webapp/,AVAILABLE}{file:/C:/Users/papu/projects/simple-webapp/src/main/webapp/}
2016-09-17 10:09:08.639:WARN:oejsh.RequestLogHandler:main: !RequestLog
2016-09-17 10:09:08.841:INFO:oejs.ServerConnector:main: Started ServerConnector@340b7ef6{HTTP/1.1}{0.0.0.0:9090}
2016-09-17 10:09:08.841:INFO:oejs.Server:main: Started @4208ms
[INFO] Started Jetty Server

Ucup Timposu
10 years ago

thanks sir

Lucky
10 years ago

Perfect. Just what I was looking for. Thanks.