Springboot generates war and deploys it to Tomcat

foreword

Today, a colleague just asked me to help him with a simple war package as a training material. It is indeed a very simple thing, but I have spent more than 2 hours in it. Because springboot has been used to generate jar packages for direct startup in recent years, I have never done a few projects to generate war and throw it on tomcat. As a result, I searched for a while, but it was nothing, nothing more than pom excluding the embedded tomcat, and the main class inheriting SpringBootServletInitializer and rewriting the configure method. But such a simple thing is actually a trap, just mark it.

simple example

Generally, it is enough to run with jar in many cases. To generate war, you need to modify pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.gdtel.iboc</groupId>
	<artifactId>HelloWorld</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<packaging>war</packaging>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.0.RELEASE</version>
	</parent>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>

		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.tomcat.embed</groupId>
			<artifactId>tomcat-embed-jasper</artifactId>
			<scope>provided</scope>
		</dependency>
	</dependencies>

	<build>
		<finalName>helloworld</finalName>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

The eighth line of the <packaging> tag contains war, which means that what we want to package and generate is a war package. Because springboot comes with tomcat, there may be a version conflict with the tomcat package to be deployed, which needs to be eliminated. Lines 21-30 solve this problem. Line 34 represents the name of the war package we want to generate.

Next, we need to modify the main class, assuming that my main class here is App.class

@SpringBootApplication
public class App extends SpringBootServletInitializer{

	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
	
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(App.class);
    }
}

Here inherit SpringBootServletInitializer and rewrite the configure method. Then execute mvn package, throw the generated war package into the webapps directory of tomcat, and start tomcat.

Pit point

The main reason for being cheated today is two points:

1. Springboot2.x or above needs to use tomcat8.5 or above . I am using tomcat8.0 which was downloaded many years ago, so no matter how much I struggle, the error is mainly reported as invalid LOC header. After checking for a long time, most of the answers are that the jar package is damaged and restarted, but no error is reported when running maven test. So that's not the problem.

2. The javaweb generated by Java EE should be placed in the webapps-javaee directory of tomcat10 (if not, create a new one). I put the webapps under the usual practice, but it couldn't run. Because webapps are distributed using Jakarta EE, the specific reason can be checked online. So either convert Java EE to Jakarta EE through a conversion tool, or put it under webapps-javaee and let tomcat convert it for you.

Guess you like

Origin blog.csdn.net/sadoshi/article/details/127944499