How to add Maven Jetty Plugin

Jetty is a Java Web Server which was developed by the Eclipse Foundation. Maven Jetty Plugin allows you to test, debug, and deploy your web application without setting up a server. This article will show you how to add Maven Jetty Plugin in your project.

Adding Jetty Plugin to your pom.xml

Visit this post to create a simple maven web application if you don’t have one. In your pom.xml, add the jetty plugin in the build > plugins section.

<build>
    <!-- other stuffs inside your build section-->

    <plugins>
        <!-- Jetty Plugin. Default port is 8080 -->
        <plugin>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-maven-plugin</artifactId>
            <version>9.4.28.v20200408</version>
        </plugin>
    </plugins>
</build>

You can change the default port of 8080 to something you want to avoid any conflicting port if you are running multiple instances of Jetty. Or add a hot-swap functionality that auto reloads Jetty to deploy the changed file.

For example, here’s our configuration that changes the port from 8080 to 8085 and scans the project every 5 seconds for any changes:

<build>
    <!-- other stuffs inside your build section-->

    <plugins>
            <!-- Jetty Plugin. Default port is 8080 -->
            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>9.4.28.v20200408</version>
                <configuration>
                    <httpConnector>
                        <port>8085</port>
                    </httpConnector>
                    <!-- scans your project for any changes to hot swap it to server -->
                    <scanIntervalSeconds>5</scanIntervalSeconds>
                </configuration>
            </plugin>
        </plugins>
</build>

Starting Jetty Plugin

Using IntelliJ, you can run jetty plugin by expanding the Maven Tab and on Plugins section, double click jetty:run.

If you want to start jetty in the command line, navigate to your root project (where your pom.xml resides) and type:

mvn jetty:run
jetty

Test it to your browser by going to localhot:8080 or 8085 or to the port that you have defined.

Other developers encountered an issue wherein IntelliJ is not showing the Jetty plugin in the Maven tab. It might be because you have added the Jetty plugin under the PluginManagement section. To resolve this, move the jetty plugin outside and just add it as a child in build > plugins section.

Share this tutorial!