Skip to content

Jetty

Run a Maven web application with the Jetty Maven plugin, change the port, and serve it over TLS with a Let's Encrypt certificate.

3 min readUpdated

Jetty is a lightweight Java web server and servlet container. Its Maven plugin starts a web application straight from the project with mvn jetty:run, which is handy during development. This note creates a sample web project, runs it with the current plugin, and then shows the legacy maven-jetty-plugin 6.x setup for a custom port, TLS and mutual TLS.

Create a web application project

Generate a minimal WAR project from the maven-archetype-webapp archetype:

Terminal window
mvn archetype:generate -DgroupId=tld.domain -DartifactId=example-server -DarchetypeArtifactId=maven-archetype-webapp -Dversion=1.0

The examples below assume <finalName>example-server</finalName> in the <build> section, so the application is served under /example-server.

Run with the current Jetty Maven plugin

Since Jetty 12 the plugin is published once per Jakarta EE version, with the EE level in its group and artifact ID. For Jakarta EE 10, add it to pom.xml:

pom.xml
<plugin>
<groupId>org.eclipse.jetty.ee10</groupId>
<artifactId>jetty-ee10-maven-plugin</artifactId>
<version>12.0.41</version>
</plugin>

Start the server from the project directory:

Terminal window
mvn jetty:run

Jetty listens on port 8080 by default. To use another port, set jetty.http.port on the command line:

Terminal window
mvn -Djetty.http.port=9090 jetty:run

The same can be configured in the plugin with an <httpConnector> element containing <port>. Since Jetty 9 an HTTPS connector can no longer be defined in pom.xml; it must be set up in Jetty XML files listed in the plugin’s jettyXmls parameter (see the Jetty documentation in the references).

Run with the legacy maven-jetty-plugin

The rest of this note uses the old org.mortbay.jetty:maven-jetty-plugin 6.1.26 (Jetty 6, servlet 2.5), where connectors, including SSL, are configured directly in the POM. Use it only for old projects.

pom.xml
<build>
<finalName>example-server</finalName>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.26</version>
</plugin>
</plugins>
</build>

Start the server:

Terminal window
mvn jetty:run

The application is then available at http://localhost:8080/example-server.

Change the port

To listen on another port, add a <configuration> with a connector:

pom.xml
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.26</version>
<configuration>
<connectors>
<connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
<port>9090</port>
</connector>
</connectors>
</configuration>
</plugin>

After mvn jetty:run the application is at http://localhost:9090/example-server.

Serve over TLS

Build a keystore from a Let’s Encrypt certificate

A certificate issued by Let’s Encrypt comes as four PEM files:

  • cert.pem: the certificate for your domain (public key)
  • chain.pem: the intermediate certificate
  • fullchain.pem: the certificate plus the intermediate chain (CA bundle)
  • privkey.pem: the private key

Combine the full chain and the private key into a PKCS#12 file. openssl asks for an export password; this example uses changeit throughout.

Terminal window
openssl pkcs12 -export -in fullchain.pem -inkey privkey.pem -out keystore.p12

Convert the PKCS#12 file to a Java keystore (JKS), which the Jetty 6 connector expects:

Terminal window
keytool -importkeystore -deststorepass changeit -destkeypass changeit -destkeystore keystore.jks -srckeystore keystore.p12 -srcstoretype PKCS12 -srcstorepass changeit

The PKCS#12 file is no longer needed:

Terminal window
rm keystore.p12

Since Java 9 the default keystore type is PKCS#12, so current Jetty versions can use keystore.p12 directly and skip the JKS conversion.

Add an SSL connector

Put keystore.jks in the example-server project directory and add this connector to the plugin’s <connectors>. The passwords must match the ones used when the keystore was created.

pom.xml
<connector implementation="org.mortbay.jetty.security.SslSocketConnector">
<port>9443</port>
<keystore>${basedir}/keystore.jks</keystore>
<password>changeit</password>
<keyPassword>changeit</keyPassword>
</connector>

Run mvn jetty:run again; the application is now at https://localhost:9443/example-server.

To see the certificate chain and handshake the server returns, connect with openssl:

Terminal window
openssl s_client -connect localhost:9443

Require client certificates (mutual TLS)

For mutual authentication the server also needs a truststore holding the certificates of trusted clients. Extend the connector:

pom.xml
<connector implementation="org.mortbay.jetty.security.SslSocketConnector">
<port>9443</port>
<keystore>${basedir}/keystore.jks</keystore>
<password>changeit</password>
<keyPassword>changeit</keyPassword>
<truststore>${basedir}/serverTruststore.jks</truststore>
<trustPassword>changeit</trustPassword>
<needClientAuth>true</needClientAuth>
</connector>

With needClientAuth set, clients that do not present a valid certificate cannot connect.

Debug the TLS handshake

TLS problems are hard to diagnose from the outside. The JSSE debug output prints every step of the handshake; enable it in the plugin’s <configuration>:

pom.xml
<systemProperties>
<systemProperty>
<name>javax.net.debug</name>
<value>ssl</value>
</systemProperty>
</systemProperties>

References

Related notes

4 min read

Jakarta EE

Jakarta EE 10 and 11: component versions, deployment descriptor headers, Faces namespaces and a multi-module project layout.

  • Java & Jakarta EE
13 min read

Jakarta Faces (JSF)

Jakarta Faces (JSF) recipes: WebSocket push, validators with injection, resource handling, component binding, java.time and scopes.

  • Java & Jakarta EE
  • Web & Frontend
3 min read

JasperReports

JasperReports tips: localise reports with resource bundles, print page X of Y, control band splitting and page breaks, and float elements.

  • Java & Jakarta EE

Type to search the whole site.