Overview
SpringBoot allows us to create a Spring Web project simply, conveniently, and quickly. The convention is better than the configuration mode, which allows us to write less configuration. The built-in tomcat makes development more comfortable, but if we need to publish the SpringBoot project to the external tomcat, how should we operate?
One, modify the packaging method of Tomcat
Change the packaging method to war in pom.xml
<packaging>war</packaging>
2. Exclude built-in tomcat dependency
Find the spring-boot-starter-web
dependent node in pom.xml and add the following code:
<!--排除内置tomcat依赖-->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
Three, add servlet-api dependency
There are two servlets to choose from:
1.java servlet-api
<!--servlet-api依赖-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
2.tomcat-servlet
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-servlet-api</artifactId>
<version>8.0.36</version>
<scope>provided</scope>
</dependency>
Just choose one of them.
Fourth, add a servlet initialization class, and rewrite the initialization method
/**
* @Description: 发布到tomcat需要添加一个servlet的初始化类
* @Author oyc
* @Date 2020/4/23 12:40 下午
*/
public class SpringBootJpaServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
//Application类,这里一定要指向原先用main方法执行的Application启动类
return application.sources(SpringbootJpaApplication.class);
}
}
Five, other settings are published to tomcat
5.1 Modify the name of the war package
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<!-- 打成war包的名称(默认是项目名称+版本.war) -->
<warName>SpringBootJpa</warName>
</configuration>
</plugin>
5.2 Packaging
In the project root directory (that is, the directory containing pom.xml), type in the command line:
mvn clean package -Dmaven.test.skip=true
Wait for the packaging to be completed, put the war package generated in the target directory into the webapps directory of tomcat, and start tomcat to automatically complete the decompression deployment.