Wednesday, August 20, 2014

How to Configure Embedded Jetty Server with Maven

Hi Guys,

Through this post, I will show how to configure the embedded jetty server plugin with maven projects. First let's see what is Jetty. Jetty is a light weight web server. In it you can deploy your web application during the development phrase.

The Jetty Maven plugin is useful for rapid development and testing. You can add it to any webapp project that is structured according to the usual Maven defaults. The plugin can then periodically scan your project for changes and automatically redeploy the webapp if any are found. This makes the development cycle more productive by eliminating the build and deploy steps.

Lets see how to setup the jetty server into maven projects

First step, Create a maven web app project.  use the following command in to create a new hello world project.


mvn archetype:generate -DgroupId=com.guru.mayoo -DartifactId=hello-webapp -DarchetypeArtifactId=maven-archetype-webapp
view raw gistfile1.sh hosted with ❤ by GitHub

Once your project creation successful, a new web application project named “hello-webapp“, and the entire project directory structure is created automatically. Then go to that project root directory and open the pom.xml and then add the following code block within the build tag.
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.10</version>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<stopKey>foo</stopKey>
<stopPort>9999</stopPort>
<connectors>
<connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
<port>8080</port>
<maxIdleTime>60000</maxIdleTime>
</connector>
</connectors>
</configuration>
</plugin>
</plugins>
view raw gistfile1.xml hosted with ❤ by GitHub

Once you add this plugin, you can deploy your hello-webapp application into jetty-server. To deploy the webapp in to jetty server, you the command as follows

mvn clean install jetty run
view raw gistfile1.txt hosted with ❤ by GitHub

then to view your web app in your browser localhost:8080/hello-webapp then try more complicated examples.