基于 JAR、WAR 的部署应用
1. 基于JAR包的部署方式
1.1 设置应用的打包方式为jar
<packaging>jar</packaging>
1.2 应用启动类
@SpringBootApplication
public class SbiaCartService {
/**
* 负责启动引导应用程序
*
* @param args 入参
*/
public static void main(String[] args) {
SpringApplication.run(SbiaCartService.class, args);
}
}
1.3 打包运行
java -jar sbia-cart-web-1.0-SNAPSHOT.jar
2. 基于WAR包的部署方式
2.1 变更应用的打包方式为war
<packaging>war</packaging>
2.2 启动类继承SpringBootServletInitializer
2.3 打包部署
现在就可以进行打包部署到tomcat容器啦!
3. 完整pom配置
<?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">
<!-- 1. change parent to spring-boot-starter-parent -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.tm.sbia</groupId>
<artifactId>sbia-cart-web</artifactId>
<version>1.0-SNAPSHOT</version>
<modelVersion>4.0.0</modelVersion>
<!-- 如果是可执行jar方式部署, packaging设置为jar -->
<packaging>war</packaging>
<name>sbia-cart-web</name>
<dependencies>
<!-- 2. add spring-boot-start-web dependency-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- change logging system to log4j2 -->
<!--<dependency>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter</artifactId>-->
<!--<exclusions>-->
<!--<exclusion>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-logging</artifactId>-->
<!--</exclusion>-->
<!--</exclusions>-->
<!--</dependency>-->
<!--<dependency>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-log4j2</artifactId>-->
<!--</dependency>-->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
4. 实现原理
配置很简单,要想深入了解其实现原理,可参考以下内容:
有关SpringBootServletInitializer的介绍可参考Spring Boot In Action - 形形色色的Initializer们。
因为war包部署方式下已经存在Tomcat Server,所以嵌入式Tomcat便不再启用了,相关逻辑可参考Spring Boot In Action - WebServer自动配置的实现及嵌入式Tomcat的启动过程。
因为启动类中仍旧保留main方法,所以该war包就可以通过java命令行启动,也可以部署到tomcat容器。