Getting Started with Maven | Web Application Development | Running Web Apps with jetty-maven-plugin

When developing a web application, it is very convenient if you can start a Java server immediately and run the built web application there for testing, instead of repeatedly creating a package and deploying the war file to a Java server.

To do this, let’s prepare a setup that lets us check operation using the servlet container “Jetty.” Of course, the task is to edit pom.xml.

Because there are several things to do this time, first here is the completed version. The completed pom.xml for this project is shown below. Some parts may be new, but for now rewrite it as shown.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.devkuma</groupId>
  <artifactId>SampleWebApp</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>SampleWebApp Maven Webapp</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <jetty.version>9.4.8.v20171121</jetty.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.eclipse.jetty</groupId>
      <artifactId>jetty-server</artifactId>
      <version>${jetty.version}</version>
    </dependency>
    <dependency>
      <groupId>org.eclipse.jetty</groupId>
      <artifactId>jetty-webapp</artifactId>
      <version>${jetty.version}</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>SampleWebApp</finalName>
    <plugins>
      <plugin>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>${jetty.version}</version>
      </plugin>
    </plugins>
  </build>
</project>

Installing and Running

After editing, build the program and install it into the local environment. Run the following command.

$ mvn install

This command installs the built program into the local repository, which manages libraries in the local environment. Here, however, it is mainly used to perform the sequence of tasks such as downloading required libraries, building the program, and creating the war file.

The first run downloads the required libraries from the central repository, so it takes some time. From the second run onward, the downloads already exist on your computer, so it does not take as long.

Running Jetty

Once the program builds successfully, run the web application using Jetty. You can do this with the following command.

$ mvn jetty:run

This command also downloads required libraries from the central repository the first time it is run. Then it starts Jetty and deploys the generated web application. When startup is complete, access the following address.

http://localhost:8080/

When the web browser opens, “Hello World!” is displayed on the screen. This is the index.jsp included by default inside webapp. With this, you can confirm that the server has started and that the web application can be accessed.

After roughly checking operation, press Ctrl+C in the command prompt or terminal to stop the command. The server then shuts down and returns to the original input-waiting state.

Properties and Variables

Now let’s look at the pom.xml we created. The first noticeable setting is the package type.

<packaging>war</packaging>

With this setting, the built program is created as a war file, not a Jar file. For web applications, <packaging> is basically war.

About Properties

Another very important part is the <properties> tag. In the pom.xml we created, it is written as follows.

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <jetty.version>9.4.8.v20171121</jetty.version>
</properties>

The <project.build.sourceEncoding> tag was already explained. It specifies the encoding of source files.

The other tag is <jetty.version>. This specifies the Jetty version. Here, version 9.4.8.v20171121, the latest version as of December 2017, is assigned to the property named jetty.version.

You can see how it is used by looking elsewhere. For example, in the <dependencies> tag, you can find the following.

<version>${jetty.version}</version>

${jetty.version} sets the value of <jetty.version>. This ${jetty.version} is a variable named jetty.version. In other words, <jetty.version> creates the jetty.version variable.

The variable name jetty.version is not predefined. It was simply given an easy-to-understand name. You can name variables however you like. For example, if you want to use a variable named “myval,” prepare its value with a <myval> tag and use it in the form ${myval}.

In pom.xml, you often prepare libraries for a specific version. In such cases, it is common to prepare a variable in advance, set the version name there, and use that variable to configure each library.

This way, if you change the version, you only need to rewrite the value of <jetty.version>. If you instead write the value separately for each library, you must find and change every value. Some libraries might accidentally remain on the old version.

Preparing properties in the <properties> tag and creating variables this way is an essential feature that can be used with many libraries and plugins.

Using Jetty

This time, we are using one of Maven’s most important features: dependency library management. Look at the <dependencies> tag. You can see that two library settings are added besides the JUnit library.

Dependency Libraries

<dependency>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-server</artifactId>
    <version>${jetty.version}</version>
</dependency>
<dependency>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-webapp</artifactId>
    <version>${jetty.version}</version>
</dependency>

These are related to using Jetty. Their roles are briefly explained below.

jetty-server

The first is the Jetty server library. It includes the basic Jetty server library.

jetty-webapp

This includes library components related to web applications, such as Jetty Webapp, Security, Servlet, and XML. Jetty server itself can start without this library, but depending on the resources used by the web application, problems may occur, so prepare it together with jetty-server.

Jetty Plugin

Besides the libraries, you also need plugin settings to start the Jetty server. This is jetty-maven-plugin, and it only needs the minimum settings: group ID, artifact ID, and version.

Jetty Plugin

<build>
    <finalName>SampleWebApp</finalName>
    <plugins>
        <plugin>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-maven-plugin</artifactId>
            <version>${jetty.version}</version>
        </plugin>
    </plugins>
</build>

Earlier we started Jetty with mvn jetty:run, and this Jetty plugin is what made that possible. It is simple because no special configuration is required. This plugin makes it possible to run a web application using Jetty.

In summary, by adding two <dependency> entries and one <plugin>, you can run a web application on Jetty. Remember this as a basic technique for web application development.