Preface
Spring Boot projects are usually run in embedded tomcat or jetty servers, and they rarely use war packages to deploy to external service containers. Even if placed in Linux, they usually start the Application class directly. However, sometimes we need to deploy to external servers, which is a bit troublesome for Spring Boot.
I won’t say much below, let’s take a look at the detailed introduction together.
Environmental Statement:
jdk:1.8
Server: Alibaba Cloud, ubuntu 16.04
springBoot:1.5.9.RELEASE
Purpose
Package springBoot into 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/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>waibao</groupId> <artifactId>project</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> </parent> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Remove tomcat support and change to compile-time support--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency> <!--Non-strict mode--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!-- Configuring the non-strict mode of thymeleaf, requires the dependency--> <dependency> <groupId>net.sourceforge.nekohtml</groupId> <artifactId>nekohtml</artifactId> <version>1.9.22</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>6.0.6</version> </dependency> <!-- Alibaba Connection Pool--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.5</version> </dependency> <!-- fastJson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.40</version> </dependency> <!-- Hot Deployment--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <version>1.5.9.RELEASE</version> <optional>true</optional> <scope>true</scope> </dependency> </dependencies> <build> <finalName>/testweb</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.4</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </build></project>
Notice
1. The maven plug-in cannot use the plug-in that comes with springBoot. I used this plugin to report the following error
Failed to execute goal org.apache.maven.plugins:maven-jar-plugin:2.6:jar (default-jar) on project project: Execution default-jar of goal org.apache.maven.plugins:maven-jar-plugin:2.6:jar failed: An API incompatibility was encountered while executing org.apache.maven.plugins:maven-jar-plugin:2.6:jar: java.lang.ExceptionInInitializerError: null
Modify the maven plugin to
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.4</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration></plugin>
It should be noted that because I don't have web.xml. So add more
<configuration> <failOnMissingWebXml>false</failOnMissingWebXml></configuration>
2.
<build> <finalName>xxx</finalName></build>
finalName needs to be consistent with context-path in application.yml.
3. You need to change the dependency of tomcat to compile time
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency>
4. SpringBoot entry class, inherit SpringBootServletInitializer and overwrite
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(MainApplication.class); }where MainApplication.class is the entry class of springBoot.
Pack
Go to the root directory of the project. If you are using IDEA, enter directly here:
mvn clean package -DskipTests
After packaging, the corresponding .war file will be generated in the target folder of the project.
Upload
Just throw the war file under tomcat's webapps.
Unresolved issues:
I originally wanted to make it into a jar package. Then start springBoot directly. Wu Nai made it into a Jar package and kept reporting errors in the maven plug-in. You can only make it into war package and upload it to tomcat.
If anyone has solved 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
renew. . . .
The problem has been resolved. You can package springBoot into Jar directly and run it.
reason:
<plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin></plugins>
The official example given by springBoot is using this plugin.
Maven will automatically download maven-jar-plugin version 2.4 and 2.6 versions.
However, Maven will rely on version 2.6 by default. . I didn't notice the error reported by the project at the beginning that maven-jar-plugin 2.6jar is incompatible. This resulted in the error not being found. .
That is, version 2.6 is incompatible. Just manually change it to version 2.4. As shown below:
<plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.4</version> </plugin></plugins>
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.