Appendix D. Deploying a Spring Boot application using WAR files
As an alternative to the supported application packaging and deployment workflow using fat JAR files, you can package and deploy a Spring Boot application as a WAR (Web Application Archive) file. You must configure your build and deployment settings to ensure that your application builds and deploys correctly on OpenShift.
Prerequisites
- A Spring Boot application, such as an example.
- Fabric8 Maven Plugin used to deploy your application to OpenShift.
- Spring Boot Maven Plugin used to package your application.
Procedure
Add
war
packaging to thepom.xml
file of your project:Example
pom.xml
<project ...> ... <packaging>war</packaging> ... </project>
Specify
spring-boot-starter-tomcat
as a dependency of your application:Example
pom.xml
<project ...> ... <dependencies> ... <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> ... </dependencies> ... </project>
Ensure the
repackage
Maven goal for the Spring Boot Maven plugin is defined in thepom.xml
file:Example
pom.xml
<project ...> ... <build> ... <plugins> ... <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> ... </project>
This ensures that the Spring Boot classes used to launch the application are included in the WAR file, and that the corresponding properties for these classes are defined in the
MANIFEST.mf
file of the WAR file:-
Main-Class: org.springframework.boot.loader.WarLauncher
-
Spring-Boot-Classes: WEB-INF/classes/
-
Spring-Boot-Lib: WEB-INF/lib/
-
Spring-Boot-Version: 2.2.11
-
Add the
ARTIFACT_COPY_ARGS
environment variable to thepom.xml
file.The Fabric8 Maven Plugin consumes this variable during the build process and ensures that the Build and Deploy tool uses the WAR file (rather than the default fat JAR file) to create the application container image:
Example
pom.xml
... <profile> <id>openshift</id> <build> <plugins> <plugin> <groupId>io.fabric8</groupId> <artifactId>fabric8-maven-plugin</artifactId> <executions> ... </executions> <configuration> <images> <image> <name>${project.artifactId}:%t</name> <alias>${project.artifactId}</alias> <build> <from>registry.access.redhat.com/redhat-openjdk-18/openjdk18-openshift:${openjdk18-openshift.version}</from> <assembly> <basedir>/deployments</basedir> <descriptorRef>artifact</descriptorRef> </assembly> <env> <ARTIFACT_COPY_ARGS>*.war</ARTIFACT_COPY_ARGS> <JAVA_APP_DIR>/deployments</JAVA_APP_DIR> </env> <ports> <port>8080</port> </ports> </build> </image> </images> </configuration> </plugin> </plugins> </build> </profile> ...
Add the
JAVA_APP_JAR
environment variable to thesrc/main/fabric8/deployment.yml
file.This variable instructs the Fabric8 Maven Plugin to launch your application using the WAR file included with the container. If
src/main/fabric8/deployment.yml
does not exist, you can create it.Example
deployment.yml
spec: template: spec: containers: ... env: - name: JAVA_APP_JAR value: ${project.artifactId}-${project.version}.war
Build and deploy your application:
mvn clean fabric8:deploy -Popenshift