SpringBoot打包成war包在tomcat运行

第一步

首先,我们需要将pom.xml中的打包方式改成war

1

<packaging>jar</packaging>

替换成

1

<packaging>war</packaging>

第二步

spring-boot-starter-tomcatscope属性设置为provided

1

2

3

4

5

<dependency>

  <groupId>org.springframework.boot</groupId>

  <artifactId>spring-boot-starter-tomcat</artifactId>

  <scope>provided</scope>

</dependency>

第三步

添加ServletInitializer

1

2

3

4

5

6

7

8

9

import org.springframework.boot.builder.SpringApplicationBuilder;

import org.springframework.boot.web.support.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {

    @Override

    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {

        return builder.sources(Application.class);

    }

}

注:上方的Application.class中的Appliation类为你的SpringBoot启动类。

第四步

在项目根目录下运行maven命令

1

mvn clean package

执行到这一步,如果控制台没有出现ERROR就打包OK了,其war包在target目录下,将其复制到tomcat/webapps目录下启动服务器即可访问

后记

如果文章有任何纰漏或者问题,请在评论区留言哈,博主会第一时间进行改正,谢谢大家

发布了69 篇原创文章 · 获赞 17 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/qq_27404929/article/details/103452452