Java

How to use Jetty to run Java web application locally?

Development of enterprise level web applications always cause some extra effort to setup various components locally. For example, if production environment runs big and robust Web Application server, like IBM WebSphere or Oracle WebLogic, you might don’t want to install and run it locally. Either for performance, configuration or licensing obstacles. There is a reasonable alternative to run and debug application locally – Eclipse Jetty application server. This is what is said about it by the Eclipse:

Eclipse Jetty is used in a wide variety of projects and products, both in development and production. Jetty can be easily embedded in devices, tools, frameworks, application servers, and clusters.

Eclipse Jetty provides a Web server and javax.servlet container, plus support for HTTP/2, WebSocket, OSGi, JMX, JNDI, JAAS and many other integrations. These components are open source and available for commercial use and distribution.

If your project is driven by Maven, then the easiest and most straight forward way is to use a jetty-maven-plugin plugin. Here is how it could lay down in the pom.xml:

<build>
<plugins>
    <plugin>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>9.2.29.v20191105</version>
        <configuration>
            <webAppSourceDirectory>${project.basedir}/src/main/webapp</webAppSourceDirectory>
        </configuration>
    </plugin>
</plugins>
</build>

This is the very minimal configuration needed to just start an app and render web content. Please check Jetty documentation for advanced setup guide.

Jetty server can be started, by executing following command in project root directory, where pom.xml is located:

mvn jetty:run

By default served content is accessible through port 8080.

Sample Maven application can be downloaded here. It is a very basic “helloworld” Jersey REST endpoint implementation. Sample endpoint can be accessed with URL: //localhost:8080/rest/hello


Advertisement

About Danas Tarnauskas

Leave a Reply

Your email address will not be published.