How do I deploy the spring boot project into an tomcat container
- 2020-06-15 09:09:25
- OfStack
Publish the ES1en-ES2en project to the tomcat container as usual web project 1
1. Modify the packaging
Set it in pom.xml
<packaging>war</packaging>
2. Remove the embedded tomcat plug-in
Find the spring-ES20en-ES21en-ES22en dependency node in ES17en. xml and add the following code.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- Remove embedment tomcat The plug-in -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
3. Add servlet-ES27en dependencies
You can do either of the following. You can choose 1
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-servlet-api</artifactId>
<version>8.0.36</version>
<scope>provided</scope>
</dependency>
4. Modify the startup class and override the initialization methods
We usually start with the main method, there is a startup class of App, the code is as follows:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
We need a configuration similar to web.xml to launch the spring context by adding one SpringBootStartApplication class to the sibling of the Application class, as follows:
/**
* Modify the startup class, inherit SpringBootServletInitializer Pay equal attention to writing configure methods
*/
public class SpringBootStartApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
// Notice that it points to the original main Method-Executed Application Start the class
return builder.sources(Application.class);
}
}
5. Package and deploy
In the project root directory (the directory containing pom.xml), type on the command line:
mvn clean package will do, and wait for the completion of packaging. [INFO] BUILD SUCCESS will be packaged successfully.
Then put the war package in target directory into webapps directory of tomcat, start tomcat, and deploy automatically.
Finally, type in the browser
http://localhost:[port number]/[package project name]/
Release success