Jeff Li

Be another Jeff

jetty-maven-plugin and https support

Introduction

jetty-maven-plugin(formerly known as maven-jetty-plugin)is a maven plugin which enables the jetty container in maven project. It is very convenient in development environment since you can start the web app by issue only one command mvn jetty:run. By default it only supports http protocol. Additional configuration is necessary if https is required. Roughly we needs:

  • A development certificate
  • Instruct the jetty plugin to use the certificate

Besides jetty-maven-plugin, another maven plugin called keytool-maven-plugin is also needed to generate the certificate. It should be noted that the plugin has evolved a lot since version 7. The configuration is different from different version.

V6

If you are still using the version 6 plugin, please refer to this blog: Configure Maven Jetty plugin for SSL communication. I tried it in my project, but it is unstable. Sometimes it works and sometimes it fails. So I have to upgrade the plugin to V8.

V8

Add following sections to the section in your pom.xml file(Pay attention to `Line 7 - Line9`, `Line 41`, `Line 47`):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<plugin>
   <groupId>org.mortbay.jetty</groupId>
   <artifactId>jetty-maven-plugin</artifactId>
   <version>8.1.11.v20130520</version>
   <configuration>
      <scanIntervalSeconds>5</scanIntervalSeconds>
      <webApp>
         <contextPath>/context</contextPath>
      </webApp>
      <connectors>
         <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
            <port>8080</port>
            <maxIdleTime>60000</maxIdleTime>
         </connector>
         <connector implementation="org.eclipse.jetty.server.ssl.SslSelectChannelConnector">
            <port>8443</port>
            <maxIdleTime>60000</maxIdleTime>
            <keystore>${project.build.directory}/jetty-ssl.keystore</keystore>
            <password>jetty8</password>
            <keyPassword>jetty8</keyPassword>
         </connector>
      </connectors>
   </configuration>
</plugin>

<plugin>
   <groupId>org.codehaus.mojo</groupId>
   <artifactId>keytool-maven-plugin</artifactId>
   <executions>
      <execution>
         <phase>generate-resources</phase>
         <id>clean</id>
         <goals>
            <goal>clean</goal>
         </goals>
      </execution>
      <execution>
         <phase>generate-resources</phase>
         <id>genkey</id>
         <goals>
            <goal>generateKeyPair</goal>
         </goals>
      </execution>
   </executions>
   <configuration>
      <keystore>${project.build.directory}/jetty-ssl.keystore</keystore>
      <dname>cn=localhost</dname>
      <keypass>jetty8</keypass>
      <storepass>jetty8</storepass>
      <alias>jetty8</alias>
      <keyalg>RSA</keyalg>
   </configuration>
</plugin>

Every thing should be ready now. If you are using Postman as your test tool, please navigate to https://loocalhost:8443/context to accept the certificate before you make further tests after running mvn jetty:run.

Comments