首页 > 代码库 > Spring Boot将WAR文件部署到Tomcat

Spring Boot将WAR文件部署到Tomcat

在本文中,将演示如何将Spring Boot WAR文件部署到Tomcat servlet容器中。

对于Spring Boot WAR部署,需要执行三个步骤:

  1. 扩展SpringBootServletInitializer
  2. 根据提供标记嵌入式servlet容器。
  3. 更新包装为 War

测试工具:

  • Spring Boot 1.4.2.RELEASE
  • Tomcat 8.5.9
  • Maven 3

注意
在Spring Boot中,具有嵌入服务器解决方案的最终可执行JAR文件可能不适合所有生产环境,特别是部署团队(具有良好的服务器优化和监控技能知识,但缺乏开发经验的团队),他们希望完全控制服务器,并且不能接触代码。

1. 推展 SpringBootServletInitializer

使现有的 @SpringBootApplication 类扩展 SpringBootServletInitializer
1.1 - 经典Spring Boot JAR部署(更新此文件以支持WAR部署)。

SpringBootWebApplication.java 文件的内容如下 -

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootWebApplication {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SpringBootWebApplication.class, args);
    }

}
Java

1.2 Spring Boot WAR 部署

SpringBootWebApplication.java 文件的内容如下 -

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

@SpringBootApplication
public class SpringBootWebApplication extends SpringBootServletInitializer {

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

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SpringBootWebApplication.class, args);
    }

}

/*@SpringBootApplication
public class SpringBootWebApplication {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SpringBootWebApplication.class, args);
    }

}*/
Java

如果要为部署创建了一个额外的SpringBootWebApplication类,请确保告诉Spring Boot要从哪个主类开始启动程序:

在 pom.xml 文件中增加以下内容指定 -

<properties>
      <start-class>com.yiibai.NewSpringBootWebApplicationForWAR</start-class>
</properties>
XML

2.提供标记嵌入式servlet容器

在 pom.xml 文件中增加以下内容指定 -

<dependencies>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

    <!-- marked the embedded servlet container as provided -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>

</dependencies>
XML

3.更新包装为War
在 pom.xml 文件中增加以下内容指定 -

<packaging>war</packaging>
XML

完成,mvn打包并将$project/target/xxx.war复制到Tomcat发布目录进行部署。

4. 完整的Spring Boot WAR + Tomcat部署

4.1 - 以Spring Boot Thymeleaf为例,更新它并手动部署到Tomcat。
4.2 - 更新现有的SpringBootWebApplication并让它扩展SpringBootServletInitializer类。

SpringBootWebApplication.java文件的完整代码如下所示 -

package com.yiibai;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

@SpringBootApplication
public class SpringBootWebApplication extends SpringBootServletInitializer {

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

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SpringBootWebApplication.class, args);
    }

}
Java

4.3 - 更新打包包,并按提供的标记spring-boot-starter-tomcat
pom.xml文件的完整代码如下所示 -

<?xml version="1.0" encoding="UTF-8"?>
<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>

    <artifactId>spring-boot-web-thymeleaf</artifactId>
    <packaging>war</packaging>
    <name>Spring Boot Web Thymeleaf Example</name>
    <description>Spring Boot Web Thymeleaf Example</description>
    <url>http://www.yiibai.com</url>
    <version>1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.2.RELEASE</version>
    </parent>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <!-- marked the embedded servlet container as provided -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

        <!-- hot swapping, disable cache for template, enable live reload -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>

        <!-- Optional, for bootstrap -->
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <version>3.3.7</version>
        </dependency>

    </dependencies>
    <build>
        <plugins>
            <!-- Package as an executable jar/war -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
XML

4.4 - 这是可选的,将contextPath更新为/yiibai以方便稍后作为演示。准备好WAR部署文件。

application.properties 文件的完整内容如下所示 -

welcome.message: Hello Yiibai

server.contextPath=/yiibai
Bash

4.5 - 获取Tomcat并部署WAR文件,执行以下命令 -

F:\worksp\springboot\spring-boot-war-tomcat>mvn clean package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Spring Boot Web Thymeleaf Example 1.0
[INFO] ------------------------------------------------------------------------
Downloading: http://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-starter-thymeleaf/1.4.2.RELEASE/spring-boot-starter-thymeleaf-1.4.2.RELEASE.pom
Downloaded: http://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-starter-thymeleaf/1.4.2.RELEASE/spring-boot-starter-thymeleaf-1.4.2.RELEASE.pom (2 KB at 0.7 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/thymeleaf/thymeleaf-spring4/2.1.5.RELEASE/thymeleaf-spring4-2.1.5.RELEASE.pom
....
.... ...
Bash

将生成的 spring-boot-web-thymeleaf-1.0.war 拷贝到 Tomcat 发布目录下 -

技术分享

例如,写本文章时,Tomcat 发布目录使用的是 - F:\worksp\springmvc\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\webapps

4.6 - 现在启动 Tomcat,访问:http://localhost:8080/yiibai/ ,程序没有错误问题,应该会看到以下结果 -

技术分享

Spring Boot将WAR文件部署到Tomcat