Spring Boot Learning 2. Deploy the project as a war package into your tomcat

Keywords: Tomcat Spring Maven log4j

Spring Boot Learning - 2. Deploy the project as a war package into your tomcat

Although Spring Boot has its own tomcat, it also provides many configuration options to configure tomcat, such as port number, access address, etc., many times it may deploy multiple projects under one tomcat. It is necessary to deploy the project as a war package into its own services. The following are the specific steps:
1. Modify the pom file to remove tomcat

 <!-- Not adopt spring-boot Self contained tomcat Start up -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>log4j-over-slf4j</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

But because tomcat is still needed for local startup, a reference to romcat is also needed.

 <!-- Local startup needs-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

Second, modify the man method to provide a SpringBootServletInitializer subclass and override its configuration method. We can change the main class of the application to inherit SpringBootServletInitializer. Or write another class.

@SpringBootApplication
public class Application  extends SpringBootServletInitializer{

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

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

}

3. Change the project packaging format to war. Modify the pom file as follows

<packaging>war</packaging>

IV. Deployment of projects to tomcat
maven's mvn package is used for packaging. After successful packaging, the war package will be generated in the target directory, deployed to the Tomcat project file (default is webapp folder), restarted tomcat, opened the browser to access the corresponding address, access address needs to add the project name. If you need to manually set the name of the war package, you can modify the project name by modifying the pom file as follows :

<!-- Support maven Packing Custom Name -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <warName>bootTest</warName>
                </configuration>
            </plugin>

Posted by Pethlehemm on Tue, 12 Feb 2019 08:45:18 -0800