back to all blogsSee all blog posts

Running a Spring Boot 3.x application WAR file on Liberty

image of author
Chuck Bridgham on Jun 15, 2023
Post available in languages:

In version 23.0.0.3 and later, you can easily run a Spring Boot 3.x application from a WAR file on Open Liberty. This post uses the Spring Boot Petclinic sample application and the Liberty Maven plugin to create and run a WAR-based Spring Boot 3.1 application on Open Liberty.

Prerequisites

  • Java 17

  • Open Liberty version 23.0.0.3 or later, with the Jakarta Servlet 6.0 (servlet-6.0) feature enabled in the server.xml file

Getting started with the Spring Petclinic sample application

This tutorial uses the 3.1 release of the Spring Petclinic sample application.

To run this application on Open Liberty, you can add the Liberty Maven plugin to the existing Petclinic pom.xml file. You can also configure a package that runs the required servlet-6.0 feature and other helpful features by default.

  1. Clone the sample repository to your machine.

    git clone https://github.com/spring-projects/spring-petclinic.git
    cd spring-petclinic
  2. Edit the Petclinic pom.xml file to build the sample application from a WAR file by using the Liberty Maven plugin.

    First, change the name of the sample from petclinic to liberty-spring-petclinic and add the war packaging type:

    <name>liberty-spring-petclinic</name>
    <packaging>war</packaging>

    Next, specify a WAR file name under the build section:

    <build>
       <finalName>liberty-spring-petclinic</finalName>

    And finally, under the <build> <plugins> section, add the Liberty Maven plugin:

    <plugin>
      <groupId>io.openliberty.tools</groupId>
      <artifactId>liberty-maven-plugin</artifactId>
      <version>3.8.1</version>
      <configuration>
          <runtimeArtifact>
              <groupId>io.openliberty</groupId>
              <artifactId>openliberty-webProfile10</artifactId>
              <version>23.0.0.5</version>
              <type>zip</type>
          </runtimeArtifact>
          <looseApplication>false</looseApplication>
      </configuration>
    </plugin>

    This example specifies a recent version of the webProfile10 package, which includes the required servlet-6.0 feature.

  3. Add the Servlet Initializer class to the Petclinic application under /src/main/java

    This class extends the SpringBootServletInitializer class and points to the sample Petclinic application.

    PetClinicServletInitializer.java

    package org.springframework.samples.petclinic;

    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

    public class PetClinicServletInitializer extends SpringBootServletInitializer {

        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(PetClinicApplication.class);
        }

    }

Now we are ready to compile and run!

Compile and run the Petclinic sample application

  1. Run the Maven clean command to clear out any existing classes from a previous compile:

    $ ./mvnw clean
  2. Compile and package the project, which produces the liberty-spring-petclinic.war file in the target directory, while skipping the tests:

    $ ./mvnw package -Dmaven.test.skip
  3. Run the following Liberty Maven plugin command to download the Liberty runtime, find and deploy the packaged liberty-spring-petclinic.war file to the dropins directory, and start the server:

    $ ./mvnw liberty:run

    Look for the sample application banner in the console:

    [INFO]               |\      _,,,--,,_
    [INFO]              /,`.-'`'   ._  \-;;,_
    [INFO]   _______ __|,4-  ) )_   .;.(__`'-'__     ___ __    _ ___ _______
    [INFO]  |       | '---''(_/._)-'(_\_)   |   |   |   |  |  | |   |       |
    [INFO]  |    _  |    ___|_     _|       |   |   |   |   |_| |   |       | __ _ _
    [INFO]  |   |_| |   |___  |   | |       |   |   |   |       |   |       | \ \ \ \
    [INFO]  |    ___|    ___| |   | |      _|   |___|   |  _    |   |      _|  \ \ \ \
    [INFO]  |   |   |   |___  |   | |     |_|       |   | | |   |   |     |_    ) ) ) )
    [INFO]  |___|   |_______| |___| |_______|_______|___|_|  |__|___|_______|  / / / /
    [INFO]  ==================================================================/_/_/_/
    [INFO]
    [INFO] :: Built with Spring Boot :: 3.1.0
  4. Test the application by visiting the following URL in a browser: http://localhost:9080/liberty-spring-petclinic/

  5. When you finish testing the application, stop the server by running the following command:

    $ ./mvnw liberty:stop

Results

You ran a sample Spring Boot 3.x application from a WAR file on Open Liberty. These steps demonstrate a simple way to create and run any WAR file from an existing Spring Boot application on Liberty.

For a closer look at the changes you make to the Petclinic sample application to run it on Open Liberty, see this change comparison on GitHub.