Maven Jetty Plugin Examples
Few Maven Jetty 8.x and 9.x plugin examples, just for quick reference.
1. Maven Jetty Plugin 9.x
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 ‘/’.
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.11.v20150529</version>
</plugin>
To run the Maven Jetty plugin
$ mvn jetty:run
Deployed URL : http://localhost:8080/
To stop the running Jetty instance
$ ctrl + c
1.2 Change a context path.
<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.
<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.
<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>
<?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
More Jetty Maven Plugin 9.x Examples
2. Maven Jetty Plugin 8.x
2.1 The ‘groupId’ is org.mortbay.jetty
, by default, it runs on port 8080, in root context ‘/’.
<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.
<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.
<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.
More Jetty Maven Plugin 8.x Examples
2. Maven Jetty Plugin 8.x
defining an external port didn’t work
very helpful. thnks
Very good…!
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
thanks sir
Perfect. Just what I was looking for. Thanks.