Package the springboot project into a war package and deploy it to tomcat

need:

The springboot project is packaged into a jar package by default. If we want to deploy the project to tomcat, we need to package the project into a war package at this time.

accomplish:

1. Modify the configuration of the pom.xml file

1.1. Add the following code:
Please add a picture description

<packaging>war</packaging>

1.2. To exclude the Tomcat that comes with the web, you only need to add the following content to the dependency of spring-boot-starter-web:
Please add a picture description

<exclusions>
  <exclusion>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
  </exclusion>
</exclusions>

1.3. Add your own Tomcat and add the following dependencies:

<dependency>
  	<groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <version>2.5.2</version>
    <scope>provided</scope>
</dependency>

1.4. Add the following plugins in build => plugins:
Please add a picture description

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-war-plugin</artifactId>
  <configuration>
  	<warName>ROOT</warName>
  </configuration>
</plugin>

Note: < warName> ROOT </warName> above indicates the name of the war package after packaging. Of course, you can change it to another name. As for the difference, I will talk about it later.

1.5. Find the startup class of the project, let it inherit a class: SpringBootServletInitializer, and override the configure method, add return builder.sources(BlogApplication.class); in the method, of course, BlogApplication.class here is my startup class name , you just need to change it to your own name. Please add a picture description
1.6, here you can package the project, click the package in maven to package, after the package is successful, a target file will be generated in the directory, and there will be a ROOT.war package under the file 1.7, copy the war package to tomcat's
Please add a picture description
webapps directory, then restart tomcat

Finally, how to access the interface in the project? If I deploy it on a remote Tomcat, for example, the ip is 192.168.66.128, then directly access 192.168.66.128:8080/interface name, if your war package is not named after ROOT, for example called demo.war, then your access path It is 192.168.66.128:8080/demo/interface name, which is also the difference I mentioned above.

Guess you like

Origin blog.csdn.net/jiangjunyuan168/article/details/121502298#comments_27526102