此内容没有您所选择的语言版本。

Appendix C. 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.
  • Fabric8 Maven Plugin used to deploy your application to OpenShift.
  • Spring Boot Maven Plugin used to package your application.

Procedure

  1. Add war packaging to the pom.xml file of your project:

    Example pom.xml

    <project ...>
      ...
      <packaging>war</packaging>
      ...
    </project>
    Copy to Clipboard Toggle word wrap

  2. 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>
    Copy to Clipboard Toggle word wrap

  3. Ensure the repackage Maven goal for the Spring Boot Maven plugin is defined in the pom.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>
    Copy to Clipboard Toggle word wrap

    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.4.9
  4. Add the ARTIFACT_COPY_ARGS environment variable to the pom.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>
        ...
    Copy to Clipboard Toggle word wrap

  5. Add the JAVA_APP_JAR environment variable to the src/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
    Copy to Clipboard Toggle word wrap

  6. Build and deploy your application:

    mvn clean fabric8:deploy -Popenshift
    Copy to Clipboard Toggle word wrap
返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat