SpringBoot打包成war包并放置在tomcat运行

it2025-01-28  15

SpringBoot打包成war包并放置在tomcat运行

SpringBoot打包成war包并放置在tomcat运行第一步:我们需要将pom.xml中的打包方式改成war标签中的东西改为war,(如果没有就建一个):第二步:找到spring-boot-starter-web依赖,改为下面所示:第三步:将spring-boot-starter-tomcat的scope属性设置为==provided==第四步:添加ServletInitializer类(类名随便取,但是必须继承SpringBootServletInitializer类)第五步:将项目进行打包。

SpringBoot打包成war包并放置在tomcat运行

第一步:我们需要将pom.xml中的打包方式改成war标签中的东西改为war,(如果没有就建一个):

  <packaging>war</packaging>

第二步:找到spring-boot-starter-web依赖,改为下面所示:

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!--忽略内嵌tomcat,打包部署到tomcat。--> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency>

第三步:将spring-boot-starter-tomcat的scope属性设置为provided

  如果没有这个依赖就请导入:

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

第四步:添加ServletInitializer类(类名随便取,但是必须继承SpringBootServletInitializer类)

  注意:这个类的位置要和springboot启动类在一个包下。   例如:

import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; public class ServletInitializer extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(DemoApplication.class); } }

  上方的DemoApplication.class中的DemoAppliation类为你的SpringBoot启动类。

第五步:将项目进行打包。

  当然也可以用maven 命令。   打包之后再target目录下就有war包了:

  将war包复制到tomcat/webapps目录下启动服务器即可访问。   注意访问地址是:   ip:端口号/war包名/路径   例如:localhost:8080/demo-0.0.1-SNAPSHOT/hello


楠哥-------一心想为IT行业添砖加瓦,却总是面向cv编程的程序员。   谢谢阅读,无误点赞,有误还望评论区指正。
最新回复(0)