SpringBoot 项目打包成 war 包

Spring Boot 默认生成独立的 JAR 文件,但有时我们需要将 Spring Boot 应用部署到传统的 Servlet 容器(如Tomcat、Jetty)中,这时就需要将 Spring Boot 应用打包成 WAR 文件。

一、修改 POM 文件

首先,确保你的项目是基于 Maven 构建的,并且需要对 pom.xml 文件进行以下几处修改:

1、更改打包方式

将默认的打包方式从 JAR 改为 WAR。

<packaging>war</packaging>

2、添加 Tomcat 依赖

在 dependencies 节点中添加 Tomcat 依赖,并将其作用域设置为 provided,这样 Tomcat 不会被打包到 WAR 文件中。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>

3、排除嵌入式 Tomcat

如果你的项目中包含嵌入式 Tomcat 依赖,需要将其排除。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>

完整 pom.xml 示例:

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
 
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
 
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
 
    <properties>
        <java.version>11</java.version>
    </properties>
 
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
 
        <!-- 其他依赖 -->
    </dependencies>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

二、修改启动类

为了支持 WAR 包部署,需要修改 Spring Boot 应用的启动类,使其继承 SpringBootServletInitializer 并重写 configure 方法。

package com.example.demo;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
 
@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {
 
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(DemoApplication.class);
    }
 
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

三、构建 WAR 包

在项目根目录下执行以下命令,使用Maven构建WAR包:

mvn clean package

构建成功后,可以在 target目录下找到生成的WAR文件,例如 demo-0.0.1-SNAPSHOT.war。

四、部署 WAR 包

将生成的 WAR 包复制到 Tomcat 的 webapps 目录下,启动 Tomcat,应用将自动部署并运行。

访问 http://localhost:8080/demo 即可访问应用。