In this tutorial, I will introduce with all of you about a Maven plugin, which can help us run a web application in Jetty with some simple configurations. This is Maven Jetty Plugin.
First of all, we need a web project using Maven for example.
I use Spring Tool Suite to create an example Maven project for this tutorial.
If you don’t know how to create a web project in Spring Tool Suite IDE, you can look at this tutorial.
Our project structure as below:
Now, I will add Maven Jetty Plugin into our project.
1 2 3 4 5 |
<plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.3.0.v20150612</version> </plugin> |
Currently, the latest version of Jetty is 9.3.0.v20150612. This is also latest version of Maven Jetty Plugin! Look at version, we can know no update for Jetty for a long time :).
OK, now we can run our application.
Let right-click on the project or the pom.xml file, then choose Run As and select Maven build…
The window Run Configurations will appear, let enter clean jetty:run into field Goals as below:
After that press Run button, Maven will build our project and run it on Jetty server with default port is 8080, root context path will be “/”, this mean you only need run http://localhost:8080 then you can see the web page.
Result:
To change the root context path of our web application, you can add more configuration for Maven Jetty Plugin in the pom.xml file as below:
1 2 3 4 5 6 7 8 9 10 |
<plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.3.0.v20150612</version> <configuration> <webApp> <contextPath>/springmvc</contextPath> </webApp> </configuration> </plugin> |
Run again, result as below:
If you want to change the port number, you also can add more configuration for Maven Jetty Plugin as below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.3.0.v20150612</version> <configuration> <scanIntervalSeconds>10</scanIntervalSeconds> <webApp> <contextPath>/springmvc</contextPath> </webApp> <httpConnector> <port>8585</port> </httpConnector> </configuration> </plugin> |
Result