SpringBoot 应用构建 WAR 包
系统环境:
- JDK 版本:1.8
- SpringBoot 版本:2.2.4.RELEASE
参考地址:
- 博文示例项目 Github 地址:https://github.com/my-dlq/blog-example/tree/master/springboot/springboot-war-example
一、简介
SpringBoot 中默认内置 Tomcat 容器,一般是构建 Jar 包方式,但是有时候需要使用外置的 Tomcat 来部署应用,这时候需要将应用打包成 War 包。
SpringBoot 打包 War 方式也很简单,下面将介绍下如何将 SpringBoot 应用打包成 War 包。
二、SpringBoot 构建 WAR 包
1、pom 中指定以 War 方式 打包
<packaging>war</packaging>2、SpringBoot Web 排除 Tomcat 依赖
SpringBoot Web 排除 Tomcat 依赖,并且单独引入 Tomcat 依赖,设置 Tomcat 依赖只应用于 编译、测试、运行 阶段,但是 打包 阶段将会移除该依赖。
<dependencies> <!--SpringBoot Web 依赖,并且排除 SpringBoot Web 中的 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> <!--添加 Tomcat 依赖,并且设置只在编译,测试,运行等时候应用 Tomcat 依赖,但是打包阶段将会移除该依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency></dependencies>3、指定构建的 WAR 包名称
<build> <!--定义 War 包名称--> <finalName>WAR包名称</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins></build>4、启动类继承 SpringBootServletInitializer 依赖
启动类中继承 SpringBootServletInitializer 类,并且配置实现 configure 方法:
@SpringBootApplicationpublic class Application extends SpringBootServletInitializer {
@Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(Application.class); }
public static void main(String[] args) { SpringApplication.run(Application.class, args); }
}三、示例应用
1、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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.4.RELEASE</version> </parent>
<groupId>mydlq.club</groupId> <artifactId>springboot-war-example</artifactId> <version>0.0.1</version> <name>springboot-war-example</name> <description>springboot war example</description>
<!--指定以 War 方式打包--> <packaging>war</packaging>
<properties> <java.version>1.8</java.version> </properties>
<dependencies> <!--SpringBoot Web 依赖,并且排除 SpringBoot Web 中的 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> <!--添加 Tomcat 依赖,并且设置只在编译,测试,运行等时候应用 Tomcat 依赖,但是打包阶段将会移除该依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> </dependencies>
<build> <!--定义 War 包名称--> <finalName>helloworld</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
</project>2、创建示例接口 Controller 类
import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;
@RestControllerpublic class TestController {
@GetMapping("/hello") public String hello1() { return "hello world!"; }
}3、SpringBoot 启动类配置
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;
@SpringBootApplicationpublic class Application extends SpringBootServletInitializer {
@Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(Application.class); }
public static void main(String[] args) { SpringApplication.run(Application.class, args); }
}四、进行测试
1、将项目构建 WAR 包
使用 Maven 执行打包
$ mvn clean package2、下载并配置 Tomcat
#下载 Tomcat$ wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v9.0.30/bin/apache-tomcat-9.0.30.tar.gz
#解压 Tomcat$ tar -zxf apache-tomcat-9.0.30.tar.gz
#重命名$ mv ./apache-tomcat-9.0.30 ./tomcat3、将 WAR 包放置到 Tomcat 进行测试
$ cp /apps/helloworld.war /usr/local/tomcat/bin/helloworld.war4、启动 Tomcat
$ /usr/local/tomcat/bin/startup.sh5、访问服务
访问示例应用的服务地址,可以看到 hello world!字样。
$ curl http://localhost:8080/helloworld/hello
hello world!6、关闭 Tomcat
测试完成,关闭 Tomcat:
$ /usr/local/tomcat/bin/shutdown.sh---END---
如果本文对你有帮助,可以关注我的公众号 "小豆丁技术栈" 了解最新动态,顺便也请帮忙 Github 点颗星哦,感谢~
