Creating a multi-module application

duration 30 minutes

Prerequisites:

You will learn how to build an application with multiple modules with Maven and Open Liberty.

What you’ll learn

A Java Platform, Enterprise Edition (Java EE) application consists of modules that work together as one entity. An enterprise archive (EAR) is a wrapper for a Java EE application, which consists of web archive (WAR) and Java archive (JAR) files. Package modules and resources into an EAR file to deploy or distribute the Java EE application to new environments.

You will learn how to establish a dependency between a web module and a Java library module. Next, use Maven to package the WAR file and the JAR file into an EAR file so that you can run and test the application on Open Liberty.

You will build a unit converter application that converts heights from centimeters into feet and inches. Enter heights in centimeters from a web page, and the application processes the input with functions in the JAR file to return the corresponding height in Imperial units.

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-multimodules.git
cd guide-maven-multimodules

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

The finish directory contains the finished project that you will build.

Access partial implementation of the application from the start folder. This folder includes a web module in the war folder, a Java library in the jar folder, and template files in the ear folder. However, the Java library and the web module are independent projects, and you will need to complete the following steps to implement the application:

  1. Add a dependency relationship between two modules.

  2. Assemble the entire application into an EAR file.

  3. Aggregate the entire build.

  4. Test the multi-module application.

Adding dependencies between WAR and JAR modules

To use the Java library in your web module, add a dependency relationship between them.

As you might notice, each module has its own pom.xml file because each module is treated as an independent project. You can rebuild, reuse, and reassemble every module on its own.

Navigate to the start directory to begin.

Replace the war/POM file.
war/pom.xml

war/pom.xml

 1<?xml version='1.0' encoding='utf-8'?>
 2<project xmlns="http://maven.apache.org/POM/4.0.0"
 3    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
 5    http://maven.apache.org/xsd/maven-4.0.0.xsd">
 6
 7    <modelVersion>4.0.0</modelVersion>
 8
 9    <groupId>io.openliberty.guides</groupId>
10    <artifactId>guide-maven-multimodules-war</artifactId>
11    <packaging>war</packaging>
12    <version>1.0-SNAPSHOT</version>
13    <name>guide-maven-multimodules-war</name>
14    <url>http://maven.apache.org</url>
15
16    <properties>
17        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
19        <maven.compiler.source>1.8</maven.compiler.source>
20        <maven.compiler.target>1.8</maven.compiler.target>
21    </properties>
22
23    <dependencies>
24        <!-- For tests -->
25        <dependency>
26            <groupId>junit</groupId>
27            <artifactId>junit</artifactId>
28            <version>4.12</version>
29            <scope>test</scope>
30        </dependency>
31
32        <!-- Provided dependencies -->
33        <dependency>
34            <groupId>javax.servlet</groupId>
35            <artifactId>javax.servlet-api</artifactId>
36            <version>3.1.0</version>
37            <scope>provided</scope>
38        </dependency>
39
40        <!-- tag::dependency[] -->
41        <dependency>
42            <groupId>io.openliberty.guides</groupId>
43            <artifactId>guide-maven-multimodules-jar</artifactId>
44            <version>1.0-SNAPSHOT</version>
45        </dependency>
46        <!-- end::dependency[] -->
47
48        <!-- Support for JDK 9 and above -->
49        <dependency>
50            <groupId>javax.xml.bind</groupId>
51            <artifactId>jaxb-api</artifactId>
52            <version>2.3.1</version>
53            <scope>test</scope>
54        </dependency>
55        <dependency>
56            <groupId>com.sun.xml.bind</groupId>
57            <artifactId>jaxb-core</artifactId>
58            <version>2.3.0.1</version>
59            <scope>test</scope>
60        </dependency>
61        <dependency>
62            <groupId>com.sun.xml.bind</groupId>
63            <artifactId>jaxb-impl</artifactId>
64            <version>2.3.2</version>
65            <scope>test</scope>
66        </dependency>
67        <dependency>
68            <groupId>javax.activation</groupId>
69            <artifactId>activation</artifactId>
70            <version>1.1.1</version>
71            <scope>test</scope>
72        </dependency>
73    </dependencies>
74
75</project>

The <dependency/> element is the Java library module that implements the functions that you need for the unit converter.

With this dependency, you can use any functions included in the library in the HeightsBean.java file of the web module.

Replace the HeightsBean class.
war/src/main/java/io/openliberty/guides/multimodules/web/HeightsBean.java

HeightsBean.java

 1// tag::copyright[]
 2/*******************************************************************************
 3 * Copyright (c) 2017, 2019 IBM Corporation and others.
 4 * All rights reserved. This program and the accompanying materials
 5 * are made available under the terms of the Eclipse Public License v1.0
 6 * which accompanies this distribution, and is available at
 7 * http://www.eclipse.org/legal/epl-v10.html
 8 *
 9 * Contributors:
10 *     IBM Corporation - Initial implementation
11 *******************************************************************************/
12// end::copyright[]
13package io.openliberty.guides.multimodules.web;
14
15public class HeightsBean implements java.io.Serializable {
16    private String heightCm = null;
17    private String heightFeet = null;
18    private String heightInches = null;
19    private int cm = 0;
20    private int feet = 0;
21    private int inches = 0;
22
23    public HeightsBean() {
24    }
25
26    // Capitalize the first letter of the name i.e. first letter after get
27    // If first letter is not capitalized, it must match the property name in
28    // index.jsp
29    public String getHeightCm() {
30        return heightCm;
31    }
32
33    public String getHeightFeet() {
34        return heightFeet;
35    }
36
37    public String getHeightInches() {
38        return heightInches;
39    }
40
41    public void setHeightCm(String heightcm) {
42        this.heightCm = heightcm;
43    }
44
45    // Need an input as placeholder, you can choose not to use the input
46    // tag::setHeightFeet[]
47    public void setHeightFeet(String heightfeet) {
48        this.cm = Integer.valueOf(heightCm);
49        // tag::getFeet[]
50        this.feet = io.openliberty.guides.multimodules.lib.Converter.getFeet(cm);
51        // end::getFeet[]
52        String result = String.valueOf(feet);
53        this.heightFeet = result;
54    }
55    // end::setHeightFeet[]
56
57    // tag::setHeightInches[]
58    public void setHeightInches(String heightinches) {
59        this.cm = Integer.valueOf(heightCm);
60        // tag::getInches[]
61        this.inches = io.openliberty.guides.multimodules.lib.Converter.getInches(cm);
62        // end::getInches[]
63        String result = String.valueOf(inches);
64        this.heightInches = result;
65    }
66    // end::setHeightInches[]
67
68}

The getFeet(cm) invocation was added to the setHeightFeet method to convert a measurement into feet.

The getInches(cm) invocation was added to the setHeightInches method to convert a measurement into inches.

Assembling multiple modules into an EAR file

To deploy the entire application on the Open Liberty server, first package the application. Use the EAR project to assemble multiple modules into an EAR file.

Navigate to the ear folder and find a template pom.xml file.

Replace the ear/POM file.
ear/pom.xml

ear/pom.xml

  1<?xml version='1.0' encoding='utf-8'?>
  2<project xmlns="http://maven.apache.org/POM/4.0.0"
  3    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
  5    http://maven.apache.org/xsd/maven-4.0.0.xsd">
  6
  7    <modelVersion>4.0.0</modelVersion>
  8
  9    <!-- tag::packaging[] -->
 10    <groupId>io.openliberty.guides</groupId>
 11    <artifactId>guide-maven-multimodules-ear</artifactId>
 12    <version>1.0-SNAPSHOT</version>
 13    <!-- tag::packagingType[] -->
 14    <packaging>ear</packaging>
 15    <!-- end::packagingType[] -->
 16    <!-- end::packaging[] -->
 17
 18    <properties>
 19        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 20        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
 21        <maven.compiler.source>1.8</maven.compiler.source>
 22        <maven.compiler.target>1.8</maven.compiler.target>
 23        <failOnMissingWebXml>false</failOnMissingWebXml>
 24        <!-- Plugin versions -->
 25        <version.liberty-maven-plugin>3.1</version.liberty-maven-plugin>
 26        <version.maven-failsafe-plugin>2.22.2</version.maven-failsafe-plugin>
 27        <version.maven-ear-plugin>3.0.2</version.maven-ear-plugin>
 28        <version.maven-compiler-plugin>3.8.1</version.maven-compiler-plugin>
 29        <!-- Liberty configuration -->
 30        <liberty.var.default.http.port>9080</liberty.var.default.http.port>
 31        <liberty.var.default.https.port>9443</liberty.var.default.https.port>
 32    </properties>
 33
 34    <dependencies>
 35        <!-- web and jar modules as dependencies -->
 36        <!-- tag::dependencies[] -->
 37        <!-- tag::dependency-jar[] -->
 38        <dependency>
 39            <groupId>io.openliberty.guides</groupId>
 40            <artifactId>guide-maven-multimodules-jar</artifactId>
 41            <version>1.0-SNAPSHOT</version>
 42            <type>jar</type>
 43        </dependency>
 44        <!-- end::dependency-jar[] -->
 45        <!-- tag::dependency-war[] -->
 46        <dependency>
 47            <groupId>io.openliberty.guides</groupId>
 48            <artifactId>guide-maven-multimodules-war</artifactId>
 49            <version>1.0-SNAPSHOT</version>
 50            <!-- tag::warType[] -->
 51            <type>war</type>
 52            <!-- end::warType[] -->
 53        </dependency>
 54        <!-- end::dependency-war[] -->
 55        <!-- end::dependencies[] -->
 56
 57        <!-- For tests -->
 58        <dependency>
 59            <groupId>org.junit.jupiter</groupId>
 60            <artifactId>junit-jupiter-engine</artifactId>
 61            <version>5.5.2</version>
 62            <scope>test</scope>
 63        </dependency>
 64
 65        <!-- Support for JDK 9 and above -->
 66        <dependency>
 67            <groupId>javax.xml.bind</groupId>
 68            <artifactId>jaxb-api</artifactId>
 69            <version>2.3.1</version>
 70            <scope>test</scope>
 71        </dependency>
 72    </dependencies>
 73
 74    <build>
 75        <finalName>${project.artifactId}</finalName>
 76        <plugins>
 77            <!-- tag::maven-ear-plugin[] -->
 78            <plugin>
 79                <groupId>org.apache.maven.plugins</groupId>
 80                <artifactId>maven-ear-plugin</artifactId>
 81                <version>${version.maven-ear-plugin}</version>
 82                <configuration>
 83                    <modules>
 84                        <!-- tag::jarModule[] -->
 85                        <jarModule>
 86                            <groupId>io.openliberty.guides</groupId>
 87                            <artifactId>guide-maven-multimodules-jar</artifactId>
 88                            <uri>/guide-maven-multimodules-jar-1.0-SNAPSHOT.jar</uri>
 89                        </jarModule>
 90                        <!-- end::jarModule[] -->
 91                        <!-- tag::webModule[] -->
 92                        <webModule>
 93                            <groupId>io.openliberty.guides</groupId>
 94                            <artifactId>guide-maven-multimodules-war</artifactId>
 95                            <uri>/guide-maven-multimodules-war-1.0-SNAPSHOT.war</uri>
 96                            <!-- Set custom context root -->
 97                            <!-- tag::contextRoot[] -->
 98                            <contextRoot>/converter</contextRoot>
 99                            <!-- end::contextRoot[] -->
100                        </webModule>
101                        <!-- end::webModule[] -->
102                    </modules>
103                </configuration>
104            </plugin>
105            <!-- end::maven-ear-plugin[] -->
106
107            <!-- Enable liberty-maven plugin -->
108            <!-- tag::liberty-maven-plugin[] -->
109            <plugin>
110                <groupId>io.openliberty.tools</groupId>
111                <artifactId>liberty-maven-plugin</artifactId>
112                <version>${version.liberty-maven-plugin}</version>
113            </plugin>
114            <!-- end::liberty-maven-plugin[] -->
115
116            <!-- Since the package type is ear,
117            need to run testCompile to compile the tests -->
118            <!-- tag::maven-compiler-plugin[] -->
119            <plugin>
120                <groupId>org.apache.maven.plugins</groupId>
121                <artifactId>maven-compiler-plugin</artifactId>
122                <version>${version.maven-compiler-plugin}</version>
123                <executions>
124                    <execution>
125                        <phase>test-compile</phase>
126                        <!-- tag::testCompile[] -->
127                        <goals>
128                            <goal>testCompile</goal>
129                        </goals>
130                        <!-- end::testCompile[] -->
131                    </execution>
132                </executions>
133            </plugin>
134            <!-- end::maven-compiler-plugin[] -->
135
136            <!-- Plugin to run integration tests -->
137            <plugin>
138                <groupId>org.apache.maven.plugins</groupId>
139                <artifactId>maven-failsafe-plugin</artifactId>
140                <version>${version.maven-failsafe-plugin}</version>
141                <configuration>
142                    <systemPropertyVariables>
143                        <default.http.port>
144                            ${liberty.var.default.http.port}
145                        </default.http.port>
146                        <default.https.port>
147                            ${liberty.var.default.https.port}
148                        </default.https.port>
149                        <cf.context.root>/converter</cf.context.root>
150                    </systemPropertyVariables>
151                </configuration>
152            </plugin>
153        </plugins>
154    </build>
155
156</project>

Set the basic configuration for the project and set the <packaging/> element to ear.

The Java library module and the web module were added as dependencies. Specify <type>war</type> for the web module. If you don’t specify the web module, Maven looks for a JAR file.

The definition and configuration of the maven-ear-plugin plug-in were added to create an EAR file. Define the <jarModule/> and <webModule/> modules to be packaged into the EAR file. To customize the context root of the application, set the <contextRoot>/converter</contextRoot> element in the <webModule/>. Otherwise, Maven automatically uses the WAR file artifactId ID as the context root for the application while generating the application.xml file.

To download and start an Open Liberty server, use the liberty-maven-plugin plug-in for Maven. This configuration is provided, and the executions of the plug-in follow the typical phases of a Maven life cycle.

To deploy the EAR application, you need to configure a server.

Create the server configuration file.
ear/src/main/liberty/config/server.xml

server.xml

 1<server description="Sample Liberty server">
 2
 3    <featureManager>
 4        <feature>jsp-2.3</feature>
 5    </featureManager>
 6
 7    <variable name="default.http.port" defaultValue="9080" />
 8    <variable name="default.https.port" defaultValue="9443" />
 9
10    <!-- tag::server[] -->
11    <httpEndpoint host="*" httpPort="${default.http.port}"
12        httpsPort="${default.https.port}" id="defaultHttpEndpoint" />
13
14    <enterpriseApplication id="guide-maven-multimodules-ear"
15        location="guide-maven-multimodules-ear.ear"
16        name="guide-maven-multimodules-ear" />
17    <!-- end::server[] -->
18</server>

Aggregating the entire build

Because you have multiple modules, aggregate the Maven projects to simplify the build process.

Create a parent pom.xml file under the start directory to link all of the child modules together. A template is provided for you.

Replace the start/POM file.
pom.xml

start/pom.xml

 1<?xml version='1.0' encoding='utf-8'?>
 2<project xmlns="http://maven.apache.org/POM/4.0.0"
 3    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
 5    http://maven.apache.org/xsd/maven-4.0.0.xsd">
 6
 7    <modelVersion>4.0.0</modelVersion>
 8
 9    <!-- tag::packaging[] -->
10    <!-- tag::groupId[] -->
11    <groupId>io.openliberty.guides</groupId>
12    <!-- end::groupId[] -->
13    <artifactId>guide-maven-multimodules</artifactId>
14    <version>1.0-SNAPSHOT</version>
15    <!-- tag::packagingType[] -->
16    <packaging>pom</packaging>
17    <!-- end::packagingType[] -->
18    <!-- end::packaging[] -->
19
20    <!-- tag::modules[] -->
21    <modules>
22        <module>jar</module>
23        <module>war</module>
24        <module>ear</module>
25    </modules>
26    <!-- end::modules[] -->
27</project>

Set the basic configuration for the project. Set pom as the <packaging/> element of the parent pom.xml file.

In the parent pom.xml file, list all of the <modules/> that you want to aggregate for the application.

Building the modules

By aggregating the build in the previous section, you can run mvn install once from the start directory and it will automatically build all your modules. This command creates a JAR file in the jar/target directory, a WAR file in the war/target directory, and an EAR file in the ear/target directory, which contains the JAR and WAR files.

Use the following command to build the entire application from the start directory:

mvn install

Since the modules are independent, you can re-build them individually by running mvn install from the corresponding module directory.

Starting the application

To deploy your EAR application on an Open Liberty server, run the Maven liberty:run goal from the ear directory:

cd ear
mvn liberty:run

Once the server is running, you can find the application at the following URL: http://localhost:9080/converter/

After you are finished checking out the application, stop the Open Liberty server by pressing CTRL+C in the shell session where you ran the server. Alternatively, you can run the liberty:stop goal from the start\ear directory in another shell session:

mvn liberty:stop

Testing the multi-module application

To test the multi-module application, add integration tests to the EAR project.

Navigate to the start\ear directory.

Create the integration test class.
src/test/java/it/io/openliberty/guides/multimodules/IT.java

IT.java

 1// tag::copyright[]
 2/*******************************************************************************
 3 * Copyright (c) 2017, 2019 IBM Corporation and others.
 4 * All rights reserved. This program and the accompanying materials
 5 * are made available under the terms of the Eclipse Public License v1.0
 6 * which accompanies this distribution, and is available at
 7 * http://www.eclipse.org/legal/epl-v10.html
 8 *
 9 * Contributors:
10 *     IBM Corporation - Initial implementation
11 *******************************************************************************/
12// end::copyright[]
13package it.io.openliberty.guides.multimodules;
14
15import static org.junit.jupiter.api.Assertions.assertEquals;
16import static org.junit.jupiter.api.Assertions.assertTrue;
17
18import java.io.BufferedReader;
19import java.io.InputStreamReader;
20import java.net.HttpURLConnection;
21import java.net.URL;
22
23import org.junit.jupiter.api.Test;
24
25public class IT {
26    String port = System.getProperty("default.http.port");
27    String war = "converter";
28    String urlBase = "http://localhost:" + port + "/" + war + "/";
29
30    @Test
31    // tag::testIndexPage[]
32    public void testIndexPage() throws Exception {
33        String url = this.urlBase;
34        HttpURLConnection con = testRequestHelper(url, "GET");
35        assertEquals(200, con.getResponseCode(), "Incorrect response code from " + url);
36        assertTrue(testBufferHelper(con).contains("Enter the height in centimeters"),
37                        "Incorrect response from " + url);
38    }
39    // end::testIndexPage[]
40
41    @Test
42    // tag::testHeightsPage[]
43    public void testHeightsPage() throws Exception {
44        String url = this.urlBase + "heights.jsp?heightCm=10";
45        HttpURLConnection con = testRequestHelper(url, "POST");
46        assertTrue(testBufferHelper(con).contains("3        inches"),
47                        "Incorrect response from " + url);
48    }
49    // end::testHeightsPage[]
50
51    private HttpURLConnection testRequestHelper(String url, String method)
52                    throws Exception {
53        URL obj = new URL(url);
54        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
55        // optional default is GET
56        con.setRequestMethod(method);
57        return con;
58    }
59
60    private String testBufferHelper(HttpURLConnection con) throws Exception {
61        BufferedReader in = new BufferedReader(
62                        new InputStreamReader(con.getInputStream()));
63        String inputLine;
64        StringBuffer response = new StringBuffer();
65        while ((inputLine = in.readLine()) != null) {
66            response.append(inputLine);
67        }
68        in.close();
69        return response.toString();
70    }
71
72}

The testIndexPage tests to check that you can access the landing page.

The testHeightsPage tests to check that the application can process the input value and calculate the result correctly.

For a Maven EAR project, the testCompile goal is specified for the maven-compiler-plugin plug-in in your ear/pom.xml file so that the test cases are compiled and picked up for execution. Run the following command to compile the test class:

mvn test-compile

ear/pom.xml

  1<?xml version='1.0' encoding='utf-8'?>
  2<project xmlns="http://maven.apache.org/POM/4.0.0"
  3    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
  5    http://maven.apache.org/xsd/maven-4.0.0.xsd">
  6
  7    <modelVersion>4.0.0</modelVersion>
  8
  9    <!-- tag::packaging[] -->
 10    <groupId>io.openliberty.guides</groupId>
 11    <artifactId>guide-maven-multimodules-ear</artifactId>
 12    <version>1.0-SNAPSHOT</version>
 13    <!-- tag::packagingType[] -->
 14    <packaging>ear</packaging>
 15    <!-- end::packagingType[] -->
 16    <!-- end::packaging[] -->
 17
 18    <properties>
 19        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 20        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
 21        <maven.compiler.source>1.8</maven.compiler.source>
 22        <maven.compiler.target>1.8</maven.compiler.target>
 23        <failOnMissingWebXml>false</failOnMissingWebXml>
 24        <!-- Plugin versions -->
 25        <version.liberty-maven-plugin>3.1</version.liberty-maven-plugin>
 26        <version.maven-failsafe-plugin>2.22.2</version.maven-failsafe-plugin>
 27        <version.maven-ear-plugin>3.0.2</version.maven-ear-plugin>
 28        <version.maven-compiler-plugin>3.8.1</version.maven-compiler-plugin>
 29        <!-- Liberty configuration -->
 30        <liberty.var.default.http.port>9080</liberty.var.default.http.port>
 31        <liberty.var.default.https.port>9443</liberty.var.default.https.port>
 32    </properties>
 33
 34    <dependencies>
 35        <!-- web and jar modules as dependencies -->
 36        <!-- tag::dependencies[] -->
 37        <!-- tag::dependency-jar[] -->
 38        <dependency>
 39            <groupId>io.openliberty.guides</groupId>
 40            <artifactId>guide-maven-multimodules-jar</artifactId>
 41            <version>1.0-SNAPSHOT</version>
 42            <type>jar</type>
 43        </dependency>
 44        <!-- end::dependency-jar[] -->
 45        <!-- tag::dependency-war[] -->
 46        <dependency>
 47            <groupId>io.openliberty.guides</groupId>
 48            <artifactId>guide-maven-multimodules-war</artifactId>
 49            <version>1.0-SNAPSHOT</version>
 50            <!-- tag::warType[] -->
 51            <type>war</type>
 52            <!-- end::warType[] -->
 53        </dependency>
 54        <!-- end::dependency-war[] -->
 55        <!-- end::dependencies[] -->
 56
 57        <!-- For tests -->
 58        <dependency>
 59            <groupId>org.junit.jupiter</groupId>
 60            <artifactId>junit-jupiter-engine</artifactId>
 61            <version>5.5.2</version>
 62            <scope>test</scope>
 63        </dependency>
 64
 65        <!-- Support for JDK 9 and above -->
 66        <dependency>
 67            <groupId>javax.xml.bind</groupId>
 68            <artifactId>jaxb-api</artifactId>
 69            <version>2.3.1</version>
 70            <scope>test</scope>
 71        </dependency>
 72    </dependencies>
 73
 74    <build>
 75        <finalName>${project.artifactId}</finalName>
 76        <plugins>
 77            <!-- tag::maven-ear-plugin[] -->
 78            <plugin>
 79                <groupId>org.apache.maven.plugins</groupId>
 80                <artifactId>maven-ear-plugin</artifactId>
 81                <version>${version.maven-ear-plugin}</version>
 82                <configuration>
 83                    <modules>
 84                        <!-- tag::jarModule[] -->
 85                        <jarModule>
 86                            <groupId>io.openliberty.guides</groupId>
 87                            <artifactId>guide-maven-multimodules-jar</artifactId>
 88                            <uri>/guide-maven-multimodules-jar-1.0-SNAPSHOT.jar</uri>
 89                        </jarModule>
 90                        <!-- end::jarModule[] -->
 91                        <!-- tag::webModule[] -->
 92                        <webModule>
 93                            <groupId>io.openliberty.guides</groupId>
 94                            <artifactId>guide-maven-multimodules-war</artifactId>
 95                            <uri>/guide-maven-multimodules-war-1.0-SNAPSHOT.war</uri>
 96                            <!-- Set custom context root -->
 97                            <!-- tag::contextRoot[] -->
 98                            <contextRoot>/converter</contextRoot>
 99                            <!-- end::contextRoot[] -->
100                        </webModule>
101                        <!-- end::webModule[] -->
102                    </modules>
103                </configuration>
104            </plugin>
105            <!-- end::maven-ear-plugin[] -->
106
107            <!-- Enable liberty-maven plugin -->
108            <!-- tag::liberty-maven-plugin[] -->
109            <plugin>
110                <groupId>io.openliberty.tools</groupId>
111                <artifactId>liberty-maven-plugin</artifactId>
112                <version>${version.liberty-maven-plugin}</version>
113            </plugin>
114            <!-- end::liberty-maven-plugin[] -->
115
116            <!-- Since the package type is ear,
117            need to run testCompile to compile the tests -->
118            <!-- tag::maven-compiler-plugin[] -->
119            <plugin>
120                <groupId>org.apache.maven.plugins</groupId>
121                <artifactId>maven-compiler-plugin</artifactId>
122                <version>${version.maven-compiler-plugin}</version>
123                <executions>
124                    <execution>
125                        <phase>test-compile</phase>
126                        <!-- tag::testCompile[] -->
127                        <goals>
128                            <goal>testCompile</goal>
129                        </goals>
130                        <!-- end::testCompile[] -->
131                    </execution>
132                </executions>
133            </plugin>
134            <!-- end::maven-compiler-plugin[] -->
135
136            <!-- Plugin to run integration tests -->
137            <plugin>
138                <groupId>org.apache.maven.plugins</groupId>
139                <artifactId>maven-failsafe-plugin</artifactId>
140                <version>${version.maven-failsafe-plugin}</version>
141                <configuration>
142                    <systemPropertyVariables>
143                        <default.http.port>
144                            ${liberty.var.default.http.port}
145                        </default.http.port>
146                        <default.https.port>
147                            ${liberty.var.default.https.port}
148                        </default.https.port>
149                        <cf.context.root>/converter</cf.context.root>
150                    </systemPropertyVariables>
151                </configuration>
152            </plugin>
153        </plugins>
154    </build>
155
156</project>

Then, enter the following command to start the server, run the tests, then stop the server:

mvn liberty:start failsafe:integration-test liberty:stop
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running it.io.openliberty.guides.multimodules.IT
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.712 sec - in it.io.openliberty.guides.multimodules.IT

Results :

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

Great work! You’re done!

You built and tested a multi-module unit converter application on the with Maven in Open Liberty.

Guide Attribution

Creating a multi-module application by Open Liberty is licensed under CC BY-ND 4.0

Copied to clipboard
Copy code block
Copy file contents

Prerequisites:

Nice work! Where to next?

What did you think of this guide?

Extreme Dislike Dislike Like Extreme Like

What could make this guide better?

Raise an issue to share feedback

Create a pull request to contribute to this guide

Need help?

Ask a question on Stack Overflow

Like Open Liberty? Star our repo on GitHub.

Star