[SpringBoot] SpringBoot -- construction and deployment of the project

Posted by disaster77 on Sun, 30 Jan 2022 22:31:23 +0100

7. Project construction and deployment

SpringBoot can be embedded with Servlet container, so it is very convenient to deploy. It can be directly packaged into executable Jar package and deployed on the server of Java allowed environment, or packaged into War package and deployed on external Tomcat server.

7.1 Jar deployment

Spring boot is packaged into Jar package. Generally, spring boot Maven plugin is used. When creating Spring Boot Web project, the plugin will be automatically displayed in POM Generated in XML file:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

After configuring the plug-in, you can create an executable Jar file. This greatly simplifies the deployment of applications, which can be allowed only by installing JRE. However, the premise is that the parent of the application is spring boot starter parent.

After the configuration is completed, double-click clean in Maven Project to execute clean, and then execute install, as shown below:


After execution, you will find that there are just packaged jar packages under the target directory of the root directory of the project. Then enter the target directory and execute the following command to start the project:

java -jar spring_security-0.0.1-SNAPSHOT.jar

Or put the jar in any drive letter, press and hold the shift + right mouse button to enter the Power Shell command to start the project.

7.2 War deployment

By default, SpringBoot is packaged as a Jar package. SpringBoot is used to construct Web applications, and built-in Tomcat is used by default. However, considering that the project needs cluster deployment or optimization, it needs to be packaged into a War package and deployed to an external Tomcat server.

  1. Modify packaging method

    Modify POM XML file changes the default Jar mode to War mode:

    <packaging>war</packaging>
    
  2. Exclude built-in Tomcat containers

    In POM To remove Tomcat from an XML file:

    <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 servlet API dependency

    In POM Add servlet API dependency to XML file:

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>
    
  4. Modify startup class

    The startup class inherits SpringBootServletInitializer and overrides the configure method:

    package com.shenziyi.spring_security;
    
    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
    
    public class WarInitializer extends SpringBootServletInitializer {
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder builder){
            //Note that you should point to the Application startup class executed by the original main method
            return builder.sources(SpringSecurityApplication.class);
        }
    }
    
  5. Packaged deployment

    The way to package the War package is the same as the way to package the Jar package. Double click clean in Maven Project to execute clean. After the clean is executed, execute install. Then put the War package under the target directory into the webapps directory of tomcat, and start Tomcat to automatically decompress and deploy. Finally, you can access the packaged project in the browser.

    Note: when using external Tomcat to deploy access, application. The configuration in properties (or application.yml) will be invalid. Please use the port of external Tomcat and the project name under webapps directory for access.

Topics: Java Spring Boot jar