At present, the architecture of front and back end separation has become the mainstream, so using SpringBoot to build applications is very fast, when the project is published to the server, just need to type a JAR package, and then run the command: java-jar jar package name can start the service;
But what about scenarios, such as deployment to an external server, where the client specifies that the service should be started as Tomcat, requiring the service provider to provide a WAR package?
At the beginning, I almost prepared to use the traditional SSM framework transformation service without searching for information. Later, after thinking and referring to relevant materials, I found that only a simple configuration and transformation can be done. The following is the process of packaging the Transformation of Springboot project into WAR, which is a simple reference:
1. Change the packaged configuration in the POM from JAR to WAR
<groupId>com.example</groupId>
<artifactId>application</artifactId>
<version>0.0.1 - the SNAPSHOT</version>
<! -- Default jar mode -->
<! --<packaging>jar</packaging>-->
<! -- change to war -->
<packaging>war</packaging>
Copy the code
2. Eliminate interference from Tomcat embedded in Springboot
<! -- War package, excluding tomcat-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
Copy the code
Some say it’s possible to add a Tomcat dependency as well, but I haven’t tried it, and most recommend the one above, which excludes the built-in Tomcat.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
Copy the code
3. Add some dependencies to POM files
<! Add servlet-API dependencies for war packages -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
Copy the code
This dependency is primarily used for integration with JSP and can be added as appropriate.
4, transformation start class
1. The way a, start the class inheritance SpringBootServletInitializer implementation configure:
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
returnbuilder.sources(Application.class); }}Copy the code
Style 2, 2. The new add a class inherits SpringBootServletInitializer implementation configure:
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
// Application. Class is a bootstrap class annotated with @SpringBootApplication
returnbuilder.sources(Application.class); }}Copy the code
5, packaging,
Reference links:
https://blog.csdn.net/qq_33512843/article/details/80951741 https://blog.csdn.net/zhangcongyi420/article/details/100065407?utm_medium=distribute.pc_relevant.none-task-blog-BlogComm endFromBaidu-1.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1.control https://blog.csdn.net/pengweismile/article/details/95897988Copy the code