Supporting mvn jetty:run in Maven applications

Supporting mvn jetty:run in Maven applications

When I’m writing a Java servlet using Maven, I find it convenient to be able to run the Java servlet in a local servlet container for testing purposes. This is very easy to do using the Jetty Maven plugin.

To add the Jetty Maven plugin to your project, modify pom.xml as follows:

<build>
    <plugins>
        <plugin>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-maven-plugin</artifactId>
            <version>9.2.0.M0</version>
        </plugin>
        ...
    </plugins>
</build>

You can then run your project in Jetty using the following command:

mvn jetty:run

If you want to run the website from the compiled WAR rather than from the source code, then use the following configuration:

<build>
    <plugins>
        <plugin>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-maven-plugin</artifactId>
            <version>9.2.0.M0</version>
            <configuration>
                <war>${basedir}/target/${project.build.finalName}.war</war>
            </configuration>
        </plugin>
        ...
    </plugins>
</build>

Then execute:

mvn jetty:run-war