spring boot project packaging

Posted by Flinch on Tue, 31 Mar 2020 16:17:18 +0200

1, Modify packing form

Set < packaging > war < / packaging > in pom.xml

2, Remove the embedded tomcat plug-in

Find the spring boot starter web dependency node in pom.xml, and add the following code,

    <!-- Remove embedded tomcat Plug-in unit -->

3, Add servlet API dependency

You can choose either of the following two ways


4, Modify the startup class and override the initialization method

We usually use the main method to start an App. The code is as follows:

public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);

We need a configuration similar to web.xml to start the spring context. Add a SpringBootStartApplication class at the same level of the Application class. The code is as follows:

 * Modify the startup class, inherit the SpringBootServletInitializer and override the configure method
public class SpringBootStartApplication extends SpringBootServletInitializer {

    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        // Note that you want to point to the Application startup class originally executed with the main method
        return builder.sources(Application.class);

5, Package deployment

In the root directory of the project (that is, the directory containing pom.xml), enter:
mvn clean package is OK. Wait for the package to be completed. If [INFO] BUILD SUCCESS appears, the package is successful.  
Then put the war package under the target directory into the webapps directory of tomcat, start tomcat, and the deployment will be automatically decompressed.  
Finally, enter in the browser

http://localhost: [port number] / [package project name]/

Released successfully

Topics: Java Tomcat xml Spring Apache