The default packaging method of springboot is jar package. The advantage of jar package is that the configuration does not need to be changed, and tomcat is embedded. If it is packaged into a jar package, it can be directly thrown into docker to run tests. However, considering that the front-end code can only be packaged into a war package, it is enough to package the back-end code into a war package and run it together with the front-end code in webapps in the tomcat container.
SpringBoot packaging format
By default, the created SpringBoot project is packaged in the jar package format. At the same time, a tomcat container is built-in.
So, if you want to package springboot into a war package and deploy it to a container such as wildfly, what should you do?
The following will be explained one by one starting from creating the project.
Creating a project
There is no difficulty in creating a project. Just use the idea tool to create a simple maven project. If your project is already a springboot project and you want to modify it It is in the form of a war package, just compare and modify it.
Modify pom.xml configuration
After creating the project, modify the pom.xml file and add the parent tag.
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.5.RELEASE</version> <relativePath/> </parent>
If it was originally a springboot project, it is already included and does not need to be modified.
Set the packaging method in pom.xml to war.
<packaging>war</packaging>
Add or modify the dependency configuration as follows:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <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> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
Among them in spring-boot-starter- The dependency on tomcat needs to be excluded in the web. At the same time, add the dependency of servlet-api. Note that the dependency added here is Servlet2.5.
Use the following plug-ins in build for packaging.
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.6</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </build>
Start main method
If there is already a main method to start, let the main method inherit SpringBootServletInitializer and implement its method. If it is a newly created project, create SpringBootApp (startup class) directly in the top-level directory, and then start the diam as follows.
@SpringBootApplication public class SpringBootApp extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(SpringBootApp.class, args); } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(SpringBootApp.class); } }
Packaging
Just execute package packaging through the maven command, and the war package can be generated in the target directory.
As for the deployment of tomcat and wildfly, you can place the war package in the corresponding directory and start the service.
Exceptions
If an exception similar to the following occurs during startup (prompts that web.xml is missing):
Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode)
Pay attention to check the pom file packaging plug-in and whether tomcat problems are eliminated, and check whether the above packaging plug-in is correct.
The reason for the problem is: the dependency package spring-boot-starter-web is referenced in the springboot project. The spring-boot-starter-tomcat referenced in this package contains the tomcat embedded servlet container, and its different versions implement different servlet version specifications.
Servlet/JSP version 2.5/2.1 supports Tomcat version 6.0.x, and the minimum java version is 1.5. Servlet/JSP version 3.0/2.2 supports Tomcat version 7.0.x, and the minimum java version is 1.6.
Since Servlet2.5 is used above, the above exception will occur when there is no web.xml file.
Solution 1: When the Servlet version is less than 3, use the /src/main/webapp/WEB-INF/web.xml file to create.
Solution 2: Add the maven-war-plugin plugin under the build of pom.xml. And set failOnMissingWebXml to false.
The above is the detailed content of How to package SpringBoot into a war package to run under tomcat or wildfly. For more information, please follow other related articles on the PHP Chinese website!