springboot默认打成jar包,但今天公司非让我打成war包,没办法,好好的内置tomcat不用,真。。。
springboot打成war包主要需要让工程屏蔽内置tomcat并且继承SpringBootServletInitializer接口。
1.建一个ServletInitializer类并且继承SpringBootServletInitializer类
注意:ServletInitializer类与springboot启动类同级目录
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(*******Application.class);
}
}
2.在pom.xml中将打包方式改为war

3.在pom.xml中移除内置tomcat
代码:
<!-- 打包war使用,移除内置tomcat -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!---->
图片:

4.为了防止项目资源加载不到,加上命名

需与properties文件中的
server.servlet.context-path=***********
保持一致
5.配置maven插件

6.先后运行maven的clean和package命令

package成功后,就可以在target目录下取到.war文件
7.非安装tomcat部署项目
将war包扔到webapps目录下

点击shutdown.bat关闭tomcat,startup.bat启动tomcat

期待指正与补充,谢谢!
本文详细介绍了如何将SpringBoot项目从默认的JAR包转换为WAR包,包括创建ServletInitializer类、修改pom.xml文件、配置Maven插件等步骤,适用于需要在外部Tomcat服务器上部署SpringBoot应用的场景。

505

被折叠的 条评论
为什么被折叠?



