The spring boot project is packaged into the whole step of war running on tomcat
- 2021-07-22 09:49:43
- OfStack
Preface
The springboot project created with maven defaults to the jar package, and springboot also comes with its own tomcat. Now you need to package the project and deploy it under the server tomcat.
Let's release the spring-boot project to the tomcat container according to the usual web project 1. The following words are not much to say, let's take a look at the detailed introduction
Step 1 Modify the packaging form
Set in pom. xml
<packaging>war</packaging>
2. Remove the embedded tomcat plug-in
Find the spring-boot-starter-web dependency node in pom. xml and add the following code,
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- Remove embedded tomcat Plug-ins -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
3. Adding servlet-api dependencies
You can do either of the following methods, and 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 method
We usually use main method to start, and 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 start the spring context, adding an SpringBootStartApplication class to the sibling of the Application class with the following code:
/**
* Modify the startup class, inherit SpringBootServletInitializer And rewrite configure Method
*/
public class SpringBootStartApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
// Note that this should point to the original use main Method executed by the Application Startup class
return builder.sources(Application.class);
}
}
Step 5 Package and deploy
At the root of the project (that is, the directory that contains pom. xml), on the command line, type:
mvn clean package
You can wait for the packaging to be completed and appear
[INFO] BUILD SUCCESS
That is, the package is successful.
Then put the war package under the target directory into the webapps directory of tomcat, start tomcat, and automatically decompress and deploy.
Finally, enter it in the browser
http://localhost: [port number]/[package project name]/
Published successfully
Summarize