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

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
5 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
5 Comment authors
me meAnthony PiñeroBhawaniUcup TimposuLucky Recent comment authors
newest oldest most voted
me me
Guest
me me

very helpful. thnks

Anthony Piñero
Guest
Anthony Piñero

Very good…!

Bhawani
Guest
Bhawani

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]… Read more »

Ucup Timposu
Guest
Ucup Timposu

thanks sir

Lucky
Guest
Lucky

Perfect. Just what I was looking for. Thanks.