English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Packaging and Deploying Spring Boot to the Tomcat on a Remote Server

Preface

Spring Boot projects usually run with embedded tomcat or jetty servers, rarely using war packages to deploy to external service containers. Even if placed in a linux environment, it is generally to start the Application class directly. However, sometimes we need to deploy to external servers, which can be a bit麻烦 for Spring Boot.

There's no more to say, let's take a look at the detailed introduction together.

Environment Declaration:

jdk:1<version>8

Server: Alibaba Cloud, ubuntu 16.04

springBoot:1<version>5<version>9.RELEASE

Purpose

The translation of the 30th segment is: 'Package springBoot to the tomcat on the remote server.'

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/boot-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>waibao</<plugin>    <groupId>org.apache.maven.plugins<
 <artifactId>project</plugin<
 artifactId>1.0-SNAPSHOT</version>
 <packaging>war</packaging>
 <parent>
  <groupId>org.springframework.boot</<plugin>    <groupId>org.apache.maven.plugins<
  <plugin>    <groupId>org.springframework.boot<-<artifactId>spring-starter-parent</plugin<
  artifactId>1<version>5<version>9.RELEASE</version>
 </parent>
 <properties>
  <java.version>1<version>8</java.version>
 </properties>
 <dependencies>
  <dependency>   <groupId>org.springframework.boot</<plugin>    <groupId>org.apache.maven.plugins<
   <plugin>    <groupId>org.springframework.boot<-<artifactId>spring-starter-web/plugin<
  </dependency>  
  <!-- Remove tomcat support and switch to compile-time support -->
  <dependency>   <groupId>org.springframework.boot</<plugin>    <groupId>org.apache.maven.plugins<
   <plugin>    <groupId>org.springframework.boot<-<artifactId>spring-starter-tomcat</plugin<
   <scope>provided</scope>
  </dependency>
  <dependency>   <groupId>org.mybatis.spring.boot/<plugin>    <groupId>org.apache.maven.plugins<
   <artifactId>mybatis-spring-<artifactId>spring-starter/plugin<
   artifactId>1<version>3<version>1</version>
  </dependency>
  <!--Non-strict mode -->
  <dependency>   <groupId>org.springframework.boot</<plugin>    <groupId>org.apache.maven.plugins<
   <plugin>    <groupId>org.springframework.boot<-<artifactId>spring-starter-thymeleaf/plugin<
  </dependency>
  <!-- To configure thymeleaf non-strict mode, this dependency is required-->
  <dependency>   <groupId>net.sourceforge.nekohtml/<plugin>    <groupId>org.apache.maven.plugins<
   <artifactId>nekohtml/plugin<
   artifactId>1<version>9<version>22</version>
  </dependency>
  <dependency>   <groupId>org.springframework.boot</<plugin>    <groupId>org.apache.maven.plugins<
   <plugin>    <groupId>org.springframework.boot<-<artifactId>spring-starter-jdbc/plugin<
  </dependency>
  <dependency>   <groupId>org.mybatis.spring.boot/<plugin>    <groupId>org.apache.maven.plugins<
   <artifactId>mybatis-spring-<artifactId>spring-starter/plugin<
   artifactId>1<version>3<version>1</version>
  </dependency>
  <dependency>   <groupId>org.springframework.boot</<plugin>    <groupId>org.apache.maven.plugins<
   <plugin>    <groupId>org.springframework.boot<-<artifactId>spring-starter-test/plugin<
   <scope>test/scope>
  </dependency>
  <dependency>
   <groupId>mysql/<plugin>    <groupId>org.apache.maven.plugins<
   <artifactId>mysql-connector-java/plugin<
   artifactId>6.0.6</version>
  </dependency>
  <!-- Ali connection pool-->
  <dependency>
   <groupId>com.alibaba</<plugin>    <groupId>org.apache.maven.plugins<
   <artifactId>druid</plugin<
   artifactId>1<version>1<version>5</version>
  </dependency>
  <!-- fastJson -->
  <dependency>
   <groupId>com.alibaba</<plugin>    <groupId>org.apache.maven.plugins<
   <artifactId>fastjson</plugin<
   artifactId>1<version>2<version>40</version>
  </dependency>
  <!-- Hot deployment-->
  <dependency>   <groupId>org.springframework.boot</<plugin>    <groupId>org.apache.maven.plugins<
   <plugin>    <groupId>org.springframework.boot<-<artifactId>spring-devtools</plugin<
   artifactId>1<version>5<version>9.RELEASE</version>
   <optional>true</optional>
   <scope>true</scope>
  </dependency>
 </dependencies>
 <build>
  <finalName>/testweb</finalName>
  version is as shown below:
   maven/<plugin>    <groupId>org.apache.maven.plugins<
    groupId>-war-jar/plugin<
    artifactId>2<version>4</version>
    <configuration>
     <failOnMissingWebXml>false</failOnMissingWebXml>
    </configuration>
   </plugin>
  </plugins>
 </build>
</project>

Note

1The maven plugin cannot use the springBoot built-in plugin. I used this plugin and encountered the following error

Failed to execute goal org.apache.maven.plugins:maven-<artifactId>maven-plugin:2<version>6:jar (default-jar) on project project: Execution default-jar of goal org.apache.maven.plugins:maven-<artifactId>maven-plugin:2<version>6:jar failed: An API incompatibility was encountered while executing org.apache.maven.plugins:maven-<artifactId>maven-plugin:2<version>6:jar: java.lang.ExceptionInInitializerError: null

Modify the maven plugin to

<plugin>
  <groupId>org.apache.maven.plugins</<plugin>    <groupId>org.apache.maven.plugins<
  groupId>-war-jar/plugin<
  artifactId>2<version>4</version>
  <configuration>
   <failOnMissingWebXml>false</failOnMissingWebXml>
  </configuration>
</plugin>

It should be noted that because I do not have web.xml, I have added

<configuration>  <failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>

2、

<build>
 <finalName>xxx</finalName>
</build>

finalName needs to be consistent with the context in application.yml-path consistent.

3、need to change the tomcat dependency to compile-time

<dependency>   <groupId>org.springframework.boot</<plugin>    <groupId>org.apache.maven.plugins<
   <plugin>    <groupId>org.springframework.boot<-<artifactId>spring-starter-tomcat</plugin<
   <scope>provided</scope>
  </dependency>

4、springBoot entry class, inheriting SpringBootServletInitializer and overriding

protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
  return application.sources(MainApplication.class);
 }

Among them, MainApplication.class is the entry class of springBoot.

Package

Go to the root directory of the project. If you are using IDEA, directly input here:

mvn clean package -DskipTests

After packaging, the corresponding .war file will be generated in the project's target folder.

Upload

You can simply drop the war file into the webapps folder under tomcat.

Unresolved issues at present:

Originally, I wanted to directly package it into a jar. Then directly start springBoot. Unfortunately, it keeps reporting errors in the maven plugin part. I can only package it into a war package and upload it to tomcat.

If someone solves this problem, please let me know.

In this article, it is said that this is a bug...

https://issues.apache.org/jira/browse/MSITE-724

Update。。。。

The problem has been solved. You can directly package springBoot into a Jar and run it.

Reason:

version is as shown below:
 <plugin>
  <groupId>org.springframework.boot</<plugin>    <groupId>org.apache.maven.plugins<
  <plugin>    <groupId>org.springframework.boot<-<artifactId>spring-boot-jar/plugin<
 </plugin>
</plugins>

the official example given by springBoot uses this plugin.

Maven will automatically download 2<version>4 version and 2<version>6version of maven-<artifactId>maven-plugin.

and Maven defaults to depend on 2<version>6At first, I didn't notice that the error reported by the project was maven-<artifactId>maven-version... 2<version>6plugin

jar incompatibility. Leading to the fact that the error cannot be found all the time... 2<version>6that is 2<version>4Version incompatibility. Just manually change it to

version is as shown below:
   <plugins>/<plugin>    <groupId>org.apache.maven.plugins<
    <plugin>    <groupId>org.springframework.boot<-<artifactId>spring-boot-jar/plugin<
   </plugin>
   maven/<plugin>    <groupId>org.apache.maven.plugins<
    groupId>-<artifactId>maven-jar/plugin<
    artifactId>2<version>4</version>
   </plugin>
</plugins>

Summary

That's all for this article. I hope the content of this article is of certain reference value to everyone's learning or work. If you have any questions, you can leave a message for communication. Thank you for your support of the呐喊 tutorial.

Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, does not edit the content manually, and does not assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (When sending an email, please replace # with @ to report, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.)

You May Also Like