Building a web application with Maven

duration 15 minutes

Learn how to build and test a simple web application using Maven and Open Liberty.

What you’ll learn

You will learn how to configure a simple web servlet application using Maven and the Liberty Maven plugin. When you compile and build the application code, Maven downloads and installs Open Liberty. If you run the application, Maven creates an Open Liberty server and runs the application on it. The application displays a simple web page with a link that, when clicked, calls the servlet to return a simple response of Hello! How are you today?.

One benefit of using Maven is that, once you have defined the details of the project and any dependencies it has, Maven automatically handles downloading and installing any and all of the dependencies.

Another benefit of using Maven is that it can run automated tests on the application after building it. You could, of course, test your application manually by starting a server and pointing a web browser at the application URL. Automated tests are a much better approach though because Maven can easily re-run the same tests each time it builds the application. If the tests don’t pass after you’ve made a change to the application, the build fails so you know that you need to fix your code.

You will create a Maven build definition file (pom.xml) for the web application project and use it to build the web application. You will then create a simple, automated test and configure Maven to run it after building the application.

Getting started

The fastest way to work through this guide is to clone the Git repository and use the projects that are provided inside:

git clone https://github.com/openliberty/guide-maven-intro.git
cd guide-maven-intro

The start directory contains the starting project that you will build upon.

The finish directory contains the finished project, which is what you will build.

Creating a simple application

The simple web application that you will build using Maven and Open Liberty is provided for you in the start directory so that you can focus on learning about Maven. The application uses the standard Maven directory structure. Using the standard directory structure saves you from customizing the pom.xml file later.

All the application source code, including the Open Liberty server configuration (server.xml), is in the src/main/liberty/config directory:

    └── src
        └── main
           └── java
           └── resources
           └── webapp
           └── liberty
                  └── config

Installing Maven

Now you’re ready to install Maven (if you haven’t already). Download the binary zip or tar.gz file and then follow the installation instructions for your operating system to extract the ZIP file and add the bin directory (which contains the mvn command) to the PATH on your computer.

Test that Maven is installed by running the following command in a terminal:

mvn -v

If Maven is installed properly, you should see information about the Maven installation similar to this:

Apache Maven 3.5.0 (ff8f5e7444045639af65f6095c62210b5713f426; 2017-04-03T20:39:06+01:00)
Maven home: /Applications/Maven/apache-maven-3.5.0
Java version: 1.8.0_131, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre
Default locale: en_GB, platform encoding: UTF-8
OS name: "mac os x", version: "10.12.6", arch: "x86_64", family: "mac"

Creating the project POM file

Before you can build the project, you define the Maven Project Object Model (POM) file, the pom.xml. Because the POM can look a bit daunting in full, create it a section at a time.

Create the pom.xml file in the current directory (at the same level as the src directory):

<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>
    <parent>
        <groupId>net.wasdev.wlp.maven.parent</groupId>
        <artifactId>liberty-maven-app-parent</artifactId>
        <version>2.0</version>
    </parent>
    <!-- Add the rest of the POM below here. -->



</project>

The pom.xml file starts with a root <project /> element and <modelversion />, which is always set to 4.0.0. The <parent /> section of the POM specifies that Maven will use the Liberty Maven parent POM to build the project. This is the minimum you technically need for the POM to be valid. But it won’t do much with this project unless you give it a bit more information about your application.

Now build up the rest of the pom.xml file, a section at a time in the space between the </parent> and </project> tags. Unless specified otherwise, add each of the sections of XML code beneath the previous one. The </project> tag must be the last thing in the pom.xml file.

A typical POM for a Liberty application contains the following sections:

  • Project coordinates: The identifiers for this application.

  • Properties (<properties />): Any properties for the project go here, including compilation details and any values that are referenced during compilation of the Java source code and generating the application.

  • Dependencies (<dependencies />): Any Java dependencies that are required for compiling, testing, and running the application are listed here.

  • Build plugins (<build />): Maven is modular and each of its capabilities is provided by a separate plugin. This is where you specify which Maven plugins should be used to build this project and any configuration information needed by those plugins.

Start by adding the project coordinates of the sample application after the closing </parent> tag:

<groupId>io.openliberty.guides</groupId>
<artifactId>ServletSample</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>

The project coordinates describe the name and version of the application. The artifactId gives a name to the web application project, which is used to name the output files that are generated by the build (e.g. the WAR file) and the Open Liberty server that is created. You’ll notice that other fields in the pom.xml file use variables that are resolved by the artifactId field. This is so that you can update the name of the sample application, including files generated by Maven, in a single place in the pom.xml file. The value of the packaging field is war so that the project output artifact is a WAR file.

Add the properties of the project:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <app.name>${project.artifactId}</app.name>
    <testServerHttpPort>9080</testServerHttpPort>
    <testServerHttpsPort>9443</testServerHttpsPort>
    <warContext>${app.name}</warContext>
    <package.file>${project.build.directory}/${app.name}.zip</package.file>
    <packaging.type>usr</packaging.type>
</properties>

The first four properties in this section just define the encoding (UTF-8) and version of Java (Java 8) that Maven uses to compile the application source code.

The next four properties provide a single place to specify values that are used in multiple places throughout the application. For example, the testServerHttpPort value is used in both the server configuration (server.xml) file and will be used in the test class that you will add (EndpointIT.java) in the application itself. This means that you can easily change the port number that the server will run on without having to update the application code in multiple places.

The final two properties define the name and type of server package Maven uses to package the application with a Liberty server.

Add details of the dependency that the web application needs in order to compile:

<dependencies>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

The HelloServlet.java class depends on javax.servlet-api to compile. Maven will download this dependency from the Maven Central repository using the groupId, artifactId, and version details that you provide here. The dependency is set to provided, which means that the API is in the server runtime and doesn’t need to be packaged by the application.

Configure the Maven plugins that build this project:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>3.2.2</version>
            <configuration>
                <packagingExcludes>pom.xml</packagingExcludes>
            </configuration>
        </plugin>
        <plugin>
            <groupId>net.wasdev.wlp.maven.plugins</groupId>
            <artifactId>liberty-maven-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <assemblyArtifact>
                    <groupId>io.openliberty</groupId>
                    <artifactId>openliberty-runtime</artifactId>
                    <version>RELEASE</version>
                    <type>zip</type>
                </assemblyArtifact>
                <serverName>${project.artifactId}Server</serverName>
                <stripVersion>true</stripVersion>
                <configFile>src/main/liberty/config/server.xml</configFile>
                <packageFile>${package.file}</packageFile>
                <include>${packaging.type}</include>
                <bootstrapProperties>
                    <default.http.port>${testServerHttpPort}</default.http.port>
                    <default.https.port>${testServerHttpsPort}</default.https.port>
                    <app.context.root>${warContext}</app.context.root>
                </bootstrapProperties>
            </configuration>
            <executions>
                <execution>
                    <id>package-server</id>
                    <phase>package</phase>
                    <goals>
                        <goal>package-server</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>target/wlp-package</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

The <build /> section gives details of the two plugins that Maven uses to build this project:

  • The Maven plugin for generating a WAR file as one of the output files.

  • The Liberty Maven plugin, which gives details of the name, version, and file type of Open Liberty runtime package which it will download from the public Maven repository.

In the liberty-maven-plugin plugin section, the <serverName /> field defines the name of the Liberty server that Maven creates. The <bootstrapProperties /> section provides the set of variables that the server.xml references. The <stripVersion /> field removes the version number from the name of the application (by default the version number of the application (1.0-SNAPSHOT) is appended to the application name). This means that the application URL remains the same even when the version number changes.

Building and running the application

To build the application, run the Maven install phase from the command line in the start directory:

mvn install

This command builds the application and creates a .war file in the target directory. It also configures and installs Open Liberty into the target/liberty/wlp directory.

Next, run the Maven liberty:start-server goal:

mvn liberty:start-server

This goal starts an Open Liberty server instance. Your Maven pom.xml is already configured to start the application in this server instance.

Testing the web application

One of the benefits of building an application with Maven is that Maven can be configured to run a set of tests. You can write tests for the individual units of code outside of a running application server (unit tests), or you can write them to call the application server directly (integration tests). In this example you will create a simple integration test that checks that the web page opens and that the correct response is returned when the link is clicked.

When Maven runs the test, it starts a test server, runs the application, runs the test, and then stops the server.

Create the test class src/test/java/io/openliberty/guides/hello/it/EndpointIT.java:

package io.openliberty.guides.hello.it;

import static org.junit.Assert.*;
import org.junit.BeforeClass;
import org.junit.Test;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;

public class EndpointIT {
    private static String URL;


    @BeforeClass
    public static void init() {

        String port = System.getProperty("liberty.test.port");
        String war = System.getProperty("war.name");
        URL = "http://localhost:" + port + "/" + war + "/" + "servlet";

    }

    @Test
    public void testServlet() throws Exception {
        HttpClient client = new HttpClient();

        GetMethod method = new GetMethod(URL);
        try {
            int statusCode = client.executeMethod(method);

            assertEquals("HTTP GET failed", HttpStatus.SC_OK, statusCode);

            String response = method.getResponseBodyAsString(1000);

            assertTrue("Unexpected response body", response.contains("Hello! How are you today?"));
        } finally {
            method.releaseConnection();
        }
    }
}

The test class name ends in IT to indicate that it contains an integration test. (Test classes with a name that ends in TEST are unit test classes.)

Configure Maven to run the integration test using the maven-failsafe-plugin by adding another <plugin /> section just before the closing </plugins> tag:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.19.1</version>
    <executions>
        <execution>
            <goals>
                <goal>integration-test</goal>
                <goal>verify</goal>
            </goals>
            <configuration>
                <includes>
                    <include>**/it/**</include>
                </includes>
                <systemPropertyVariables>
                    <liberty.test.port>${testServerHttpPort}</liberty.test.port>
                    <war.name>${warContext}</war.name>
                </systemPropertyVariables>
            </configuration>
        </execution>
    </executions>
</plugin>

The <systemPropertyVariables /> section defines some variables that the test class uses. The test code needs to know where to find the application that it is testing. While the port number and context root information can be hardcoded in the test class, it is better to specify it in a single place like the Maven pom.xml file because this information is also used by other files in the project. The <systemPropertyVariables /> section passes these details to the Java test program as a series of system properties, resolving the liberty.text.port and war.name variables.

The following lines in the Endpoint.java test class uses these system variables to build up the URL of the application:

String port = System.getProperty("liberty.test.port");
String war = System.getProperty("war.name");
URL = "http://localhost:" + port + "/" + war + "/" + "servlet";

In the test class, after defining how to build the application URL, the @Test annotation indicates the start of the test method.

In the try block of the test method, an HTTP GET request to the URL of the application returns a status code. If the response to the request includes the string Hello! How are you today?, the test passes. If that string is not in the response, the test fails. The HTTP client then disconnects from the application.

try {
    int statusCode = client.executeMethod(method);

    assertEquals("HTTP GET failed", HttpStatus.SC_OK, statusCode);

    String response = method.getResponseBodyAsString(1000);

    assertTrue("Unexpected response body", response.contains("Hello! How are you today?"));
} finally {
    method.releaseConnection();
}

In the import statements of this test class, you’ll notice that the test has some new dependencies. Before the test can be compiled by Maven, you need to update the pom.xml to include these dependencies.

Add the following two <dependency /> elements after the existing dependency in the <dependencies /> section of the pom.xml:

<dependency>
    <groupId>commons-httpclient</groupId>
    <artifactId>commons-httpclient</artifactId>
    <version>3.1</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.2.11</version>
</dependency>
<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-core</artifactId>
    <version>2.2.11</version>
</dependency>
<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-impl</artifactId>
    <version>2.2.11</version>
</dependency>
<dependency>
    <groupId>javax.activation</groupId>
    <artifactId>activation</artifactId>
    <version>1.1.1</version>
</dependency>

The Apache commons-httpclient and junit dependencies are needed to compile and run the integration test EndpointIT class. The scope for each of the dependencies is set to test because the libraries are needed only during the Maven build and do not needed to be packaged with the application.

Now, during the Maven build, Maven creates the WAR file that contains the web application and then runs any integration test classes (classes with names that end in IT) that it finds.

The directory structure of the project should now look like this:

    └── src
        ├── main
        │  └── java
        │  └── resources
        │  └── webapp
        │  └── liberty
        │         └── config
        └── test
            └── java

You can now run the command mvn install to rebuild the application, including the test you’ve added, run the test, and see that the test passes. The Maven build takes a little longer than before the test existed, but expect to see the following information in the output:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running io.openliberty.guides.hello.it.EndpointIT
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.255 sec - in io.openliberty.guides.hello.it.EndpointIT

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

To see whether the test detects a failure, change the response string in the servlet src/main/java/io/openliberty/guides/hello/HelloServlet.java so that it doesn’t match the string that the test is looking for. Then re-run the Maven build and check that the test fails.

The complete pom.xml should now look like this:

<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>
    <parent>
        <groupId>net.wasdev.wlp.maven.parent</groupId>
        <artifactId>liberty-maven-app-parent</artifactId>
        <version>2.0</version>
    </parent>
    <!-- Add the rest of the POM below here. -->
    <groupId>io.openliberty.guides</groupId>
    <artifactId>ServletSample</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <app.name>${project.artifactId}</app.name>
        <testServerHttpPort>9080</testServerHttpPort>
        <testServerHttpsPort>9443</testServerHttpsPort>
        <warContext>${app.name}</warContext>
        <package.file>${project.build.directory}/${app.name}.zip</package.file>
        <packaging.type>usr</packaging.type>
    </properties>
    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>commons-httpclient</groupId>
            <artifactId>commons-httpclient</artifactId>
            <version>3.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.2.11</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-core</artifactId>
            <version>2.2.11</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.2.11</version>
        </dependency>
        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
            <version>1.1.1</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.2.2</version>
                <configuration>
                    <packagingExcludes>pom.xml</packagingExcludes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>net.wasdev.wlp.maven.plugins</groupId>
                <artifactId>liberty-maven-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <assemblyArtifact>
                        <groupId>io.openliberty</groupId>
                        <artifactId>openliberty-runtime</artifactId>
                        <version>RELEASE</version>
                        <type>zip</type>
                    </assemblyArtifact>
                    <serverName>${project.artifactId}Server</serverName>
                    <stripVersion>true</stripVersion>
                    <configFile>src/main/liberty/config/server.xml</configFile>
                    <packageFile>${package.file}</packageFile>
                    <include>${packaging.type}</include>
                    <bootstrapProperties>
                        <default.http.port>${testServerHttpPort}</default.http.port>
                        <default.https.port>${testServerHttpsPort}</default.https.port>
                        <app.context.root>${warContext}</app.context.root>
                    </bootstrapProperties>
                </configuration>
                <executions>
                    <execution>
                        <id>package-server</id>
                        <phase>package</phase>
                        <goals>
                            <goal>package-server</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>target/wlp-package</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.19.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                        <configuration>
                            <includes>
                                <include>**/it/**</include>
                            </includes>
                            <systemPropertyVariables>
                                <liberty.test.port>${testServerHttpPort}</liberty.test.port>
                                <war.name>${warContext}</war.name>
                            </systemPropertyVariables>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>



</project>

Great work! You’re done!

You built and tested a web application project with an Open Liberty server using Maven.

You can quickly create this project structure by using the Liberty App Accelerator accelerator or the Maven liberty-archetype-webapp archetype.

Contribute to this guide

Is something missing or broken? Raise an issue, or send us a pull request.