Launching Open Liberty From A Maven Project

In a previous post, I showed the simplest possible Jakarta EE 8 project and how it could be built with Maven. This application depends upon having a Jakarta EE 8 Application Server already and installed, that the application can be deployed to. Many application servers, including Open Liberty, have the ability to automatically download and configure the application server via Maven. In this post, I'll show how to do this with Open Liberty.

Open Liberty Maven Plugin

To enable Open Liberty to be installed and configured via Maven, we must first add the liberty-maven-plugin into the project's pom.xml file.

<build>
  <finalName>${project.artifactId}</finalName>
  <plugins>
    <plugin>
      <groupId>io.openliberty.tools</groupId>
      <artifactId>liberty-maven-plugin</artifactId>
      <version>3.1</version>
    </plugin>
  </plugins>
</build>

Note, in the above snippet, I also set the project's final name to be the same as the project's artifact id. This stops the version number being automatically appended to the resulting war file, making it easier to configure within Open Liberty.

That's all that is required to configure within the pom.xml file to allow us to control and install Open Liberty from Maven.

Open Liberty Server Definition

The next stage is to configure the Open Liberty application server, for example, what application will we deploy, what will be its context url, what port will the server listen on, etc..

To configure these options, create a file src/main/liberty/config/server.xml within the Maven project.

<server>
  <featureManager>
    <feature>servlet-4.0</feature>
  </featureManager>

  <httpEndpoint httpPort="9080" httpsPort="9443"
    id="defaultHttpEndpoint" host="*" />

  <webApplication id="simpleliberty"
    location="simpleliberty.war" contextRoot="simpleliberty" />
</server>

In this fragment, you can see that we have configured the servlet-4.0 feature set to be available. We've configured the http and https ports and defined an application called simpleliberty to be deployed from simpleliberty.war and to have a context root of /simpleliberty.

Note: you can see now why I changed the application name to be the same as the artifact id and no not include the version number. Without including the version number on the war file, it becomes much easier to define the server.

Running the application

Now that we've defined the Maven build and the Open Liberty server, we can build and run the code directly from Maven.

> mvn liberty:run
.
.
[INFO] Launching defaultServer (Open Liberty 19.0.0.12/wlp-1.0.35.cl191220191120-0300) on OpenJDK 64-Bit Server VM, version 11.0.5+10 (en_GB)
[INFO] [AUDIT   ] CWWKE0001I: The server defaultServer has been launched.
[INFO] [AUDIT   ] CWWKZ0058I: Monitoring dropins for applications.
[INFO] [AUDIT   ] CWWKT0016I: Web application available (default_host): http://192.168.0.12:9080/simpleliberty/
[INFO] [AUDIT   ] CWWKZ0001I: Application simpleliberty started in 0.265 seconds.
[INFO] [AUDIT   ] CWWKF0012I: The server installed the following features: [servlet-4.0].
[INFO] [AUDIT   ] CWWKF0011I: The defaultServer server is ready to run a smarter planet. The defaultServer server started in 2.561 seconds.

As you can see, this is a simple way to configure development with Open Liberty and Maven without having to install a server beforehand. This allows you to easily get started with development and easily distribute a development environment to other people.

Credits

Photo by SpaceX on Unsplash

No Comments Yet