The premise

This article is the “fifth” article on the SpringBoot2.x Introduction album, using SpringBoot version 2.3.1.RELEASE with JDK version 1.8.

This article analyzes a content biased to operation and maintenance: the packaging and startup of SpringBoot applications. It will analyze the packaging and startup of applications under embedded Servlet containers and non-embedded Servlet containers respectively. The Servlet container takes Tomcat, which is commonly used, as an example.

Embedded Tomcat package and startup

Embedded Tomcat comes with the spring-boot-starter-Web starter, so there is no need to change dependencies on the Servlet container. A new start class club. Throwable. Ch4. Ch4Application:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Ch4Application {
  public static void main(String[] args) {  SpringApplication.run(Ch4Application.class, args);  } } Copy the code

Add a master configuration file application.properties:

server.port=9094
spring.application.name=ch4-embedded-tomcat-deploy
Copy the code

Then add the Maven plugin spring-boot-Maven-plugin to the project’s pop.xml:

<plugins>
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <version>${spring.boot.version}</version>
 <executions>  <execution>  <goals>  <goal>repackage</goal>  </goals>  </execution>  </executions>  </plugin> </plugins> Copy the code

MVN clean compile package MVN clean compile package MVN clean compile package MVN clean compile package MVN clean compile package MVN clean compile package


The console prints “BUILD SUCCESS” if the compile and package commands were successfully executed:


At the same time, the project’s target directory (in addition to some compiled class files) contains a Jar package and a x.jar. Original file:


The Jar file is a runnable file that can be run with the command (make sure the JDK is installed and add the bin directory of the JRE to your system’s Path) :

java -jar ch4-embedded-tomcat-deploy.jar
Copy the code

The console output is as follows:


The usual Jar execution command is:

Java [VM_OPTIONS] -jar Application name. Jar [SPRING_OPTIONS]Such as:java -Xms1g -Xmx2g -jar ch4-embedded-tomcat-deploy.jar --spring.profiles.active=default
Copy the code

The above command will cause the application to be suspended on the console, and the application will be Shutdown when you exit the console. On Linux, you can use the nohup command to run the Jar application without hanging up, for example:

nohup java -Xms1g -Xmx2g -jar ch4-embedded-tomcat-deploy.jar --spring.profiles.active=default >/dev/null 2>&1 &
Copy the code

Packaging and startup of non-embedded Tomcat

In general, non-embedded Tomcat needs to be packaged as a WAR file and run in an external Tomcat service.

  • First of all,removeoffspring-boot-starter-webEmbedded in dependenciesTomcatRelated dependencies, and introducedservlet-apiRely on.
  • Also set the packaging method towar(<packaging>jar</packaging>Replace with<packaging>war</packaging>).
  • And then finally upgrademaven-war-pluginPlug-ins avoid because of missingweb.xmlThe file failed to be packaged.

In SpringBoot:2.3.1.RELEASE, this version is 9.0.36. In SpringBoot:2.3.1. Pom.xml has the following dependencies:

<packaging>war</packaging>
<dependencies>
    <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>  <dependency>  <groupId>org.apache.tomcat</groupId>  <artifactId>tomcat-servlet-api</artifactId>  <version>9.0.36</version>  <scope>provided</scope>  </dependency> </dependencies> <build>  <finalName>ch3-tomcat-deploy</finalName>  <plugins>  <plugin>  <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-maven-plugin</artifactId>  <version>${spring.boot.version}</version>  <executions>  <execution>  <goals>  <goal>repackage</goal>  </goals>  </execution>  </executions>  </plugin>  <plugin>  <groupId>org.apache.maven.plugins</groupId>  <artifactId>maven-war-plugin</artifactId>  <version>3.3.0</version>  </plugin>  </plugins> </build> Copy the code

Instead of excluding spring-boot-starter-tomcat, you can reduce its scope to provided to avoid introducing additional servlet-API dependencies:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
 <dependency>  <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-starter-tomcat</artifactId>  <scope>provided</scope>  </dependency> </dependencies> Copy the code

A new start class club. Throwable. Ch3. Ch3Application, must inherit SpringBootServletInitializer and rewrite the configure () method performs the entrance class:

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class Ch3Application extends SpringBootServletInitializer {   public static void main(String[] args) {  SpringApplication.run(Ch3Application.class, args);  }   @Override  protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {  return builder.sources(Ch3Application.class);  } } Copy the code

Then use the command MVN clean compile package to package:


Download Tomcat9.0.36, download address is https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.36/bin (because of the development machine system is a 64 – bit Windows 10) :


After decompressing Tomcat, copy ch3-tomcat-deploy.war to the webapps directory and use bin/startup.bat to start Tomcat:


Due to the application. The properties inside management port and service context path configuration will be failure, need from Tomcat entry access services, such as http://localhost:8080/ch3-tomcat-deploy/.

summary

This article introduces the Jar and War packaging and deployment methods of SpringBoot respectively. In fact, the Jar package is more recommended, because the embedded container is relatively simple for development and publishing, and it is the default startup mode of SpringBoot, which supports the integration of static resources into Jar packages by default. Direct access. In large applications where the front and back ends are separated, a relatively lightweight deployment that can run directly away from the external container is much more desirable.

Project Warehouse:

  • Github:https://github.com/zjcscut/spring-boot-guide

(C-2-D E-A-20200709 1:15 AM)