前言
SpringBoot默认达成jar包,默认使用内置的Tomcat。但考虑到项目需要打成war包部署到外部的Tomcat服务器中。把SpringBoot项目按照平常的web项目一样发布到tomcat容器下。
1、修改pom.xml文件将默认的jar(默认创建项目的时候没有packaging)方式改为war。
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example.mybaties</groupId>
<artifactId>springbootmybaties</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springbootmybaties</name>
<description>Demo project for Spring Boot</description>
<!--默认为jar方式-->
<!--<packaging>jar</packaging>-->
<!--改为war方式-->
<packaging>war</packaging>
2、排除内置的Tomcat容器
1)第一种方式
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--排除内置的Tomcat容器-->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
2)第二种方式
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
3、继承org.springframework.boot.web.servlet.support.SpringBootServletInitializer,重写configure方法。
1)第一种方式,启动类继承SpringBootServletInitializer重写configure
package com.example.mybaties;
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 SpringbootmybatiesApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(SpringbootmybatiesApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(SpringbootmybatiesApplication.class);
}
}
2)新增加一个类继承SpringBootServletInitializer重写configure
package com.example.mybaties.config;
import com.example.mybaties.SpringbootmybatiesApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
/**
* @author LST
* @version 1.0
* @Description:
* @date 2020-1-17 16:19
*/
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
//此处的Application.class为带有@SpringBootApplication注解的启动类
return builder.sources(SpringbootmybatiesApplication.class);
}
}
4、为了防止应用上下文所导致的项目访问资源加载不到的问题,建议pom.xml文件中 标签下添加 标签
<build>
<!--上下文路径-->
<finalName>mybaties</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
5、打成war包
先clean再打包:mvn clean package -Dmaven.test.skip=true,也可使用idea自带的打包