Supporting mvn jetty:run in Maven applications
Maven java jetty maven
Published: 2014-04-30
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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<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:

1
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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
<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:

1
mvn jetty:run-war