Spring Boot Deploy WAR file to Tomcat

摘要: In this article, we will show you how to create a Spring Boot traditional WAR file and deploy to a Tomcat servlet container.

In this article, we will show you how to create a Spring Boot traditional WAR file and deploy to a Tomcat servlet container.

In Spring Boot, to create a WAR for deployment, we need 3 steps:

  1. Extends SpringBootServletInitializer
  2. Marked the embedded servlet container as provided.
  3. Update packaging to war

Tested with

  • Spring Boot 2.1.2.RELEASE
  • Tomcat 8 and 9
  • Maven 3
Note
In Spring Boot, the new final executable JAR file with embedded server solution may not suitable in all production environments, especially the deployment team (a team with good knowledge of server optimization and monitoring skills, but lack of, the development experience), they want full control of the server, and they don’t touch code, so, we need a traditional WAR file.

1. Extends SpringBootServletInitializer

Update the @SpringBootApplication class to extend SpringBootServletInitializer, and override the configure method.

1.1 Classic Spring Boot JAR deployment.

StartWebApplication.java
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class StartWebApplication {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(StartWebApplication.class, args);

1.2 For WAR deployment.

StartWebApplication.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
@SpringBootApplication
public class StartWebApplication extends SpringBootServletInitializer {
    public static void main(String[] args) {
        SpringApplication.run(StartWebApplication.class, args);
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(StartWebApplication.class);
/*@SpringBootApplication
public class StartWebApplication {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(StartWebApplication.class, args);
}*/

For multiple main classes, make sure tell Spring Boot which main class to start :

pom.xml
  <properties>
      <!-- The main class to start by executing java -jar -->
      <start-class>com.mkyong.SpringBootWebApplication</start-class>
  </properties>

Read this – Spring Boot – Which main class to start

2. Marked the embedded servlet container as provided

pom.xml
<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-thymeleaf</artifactId>
	</dependency>
	<!-- marked the embedded servlet container as provided -->
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-tomcat</artifactId>
		<scope>provided</scope>
	</dependency>
</dependencies>

3. Update packaging to war

pom.xml
  <packaging>war</packaging>

Done, build the project and copy the WAR file for deployment.

上一篇: Eclipse Ctrl + T in IntelliJ IDEA
下一篇: Java Digital Signatures example
 评论 ( What Do You Think )
名称
邮箱
网址
评论
验证
   
 

 


  • 微信公众号

  • 我的微信

站点声明:

1、一号门博客CMS,由Python, MySQL, Nginx, Wsgi 强力驱动

2、部分文章或者资源来源于互联网, 有时候很难判断是否侵权, 若有侵权, 请联系邮箱:summer@yihaomen.com, 同时欢迎大家注册用户,主动发布无版权争议的 文章/资源.

3、鄂ICP备14001754号-3, 鄂公网安备 42280202422812号