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,

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <!-- Remove embedded tomcat Plug-in unit -->
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>

3, Add servlet API dependency

You can choose either of the following two ways

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-servlet-api</artifactId>
    <version>8.0.36</version>
    <scope>provided</scope>
</dependency>

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:

@SpringBootApplication
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 {

    @Override
    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