[springboot] deploy war package to external tomcat container (including video)


Deploy the war package to an external tomcat container

1. Modify the packaging method

<packaging>war</packaging>

Add the above code to the beginning of the pom.xml file, as follows:
insert image description here

2. Exclude the built-in tomcat dependencies

We use an external tomcat, and naturally exclude the related jars of the built-in embedded 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>

3. Add a new class to inherit SpringBootServletInitializer to implement configure:

Why inherit this class, SpringBootServletInitializer source code comment:
Note that a WebApplicationInitializer is only needed if you are building a war file and deploying it.
If you prefer to run an embedded web server then you won't need this at all.
Note that if you is building the WAR file and deploying it, the WebApplicationInitializer is required. If you like to run an embedded web server, then you don't need this at all.

public class ServletInitializer extends SpringBootServletInitializer {
    
     
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
    
    
        //此处的Application.class为带有@SpringBootApplication注解的启动类
        return builder.sources(BootLaunchApplication.class);
    } 
}

Note:
When using external Tomcat deployment access, the following configuration in application.properties (or application.yml) will be invalid. Please use the external tomcat port and the project name under tomcat's webapps to access.

server.port=
server.servlet.context-path=

4. The build must have a finalName tag

The build code segment in pom.xml must have the name of the final build package of the application.

    <finalName>boot-launch</finalName>

5. Packaging and running

The war package is packaged, and the packaged result will be stored in the target directory of the project.

mvn clean package -Dmaven.test.skip=true

Then copy the war package to the external Tomcat webapps directory. Run in the external tomcat: execute startup.bat (windows) or startup.sh (linux) in the ${Tomcat_home}/bin/ directory, and then access the application through a browser to test the effect.

have to be aware of is

  • Unzip the boot-launch.war in the tomcat webapps directory to the boot-launch folder. So when you access the application, you must use http://localhost:8888/boot-launch/template/jsp, not: http://localhost:8888/template/jsp. A 404 error will be reported.
  • The jsp static resource reference must also be: /boot-launch/image/xxxx.png, not /image/xxxx.png
  • In the war package of JSP, the resource usage of webjars is no longer supported

Guess you like

Origin blog.csdn.net/hanxiaotongtong/article/details/122892953
Recommended