SpringBoot打包发布到tomcat

本文详细介绍如何将SpringBoot项目打包成war文件并部署到Tomcat服务器上,包括修改pom.xml配置、调整依赖范围、集成SpringBootServletInitializer以及最终的打包流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SpringBoot打包发布到tomcat

步骤一:
在pom.xml中设置<packging>war</packging>

步骤二:
将spring-boot-starter-tomcat的范围设置为provided,这样打包时就会将其排除

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

步骤三:
需要集成SpringBootServletInitializer,然后重写configure,将Spring Boot的入口类设置进去。
也就是让@SpringBootApplication 修饰的类 继承 SpringBootServletInitializer

import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.test.autoconfigure.web.servlet.SpringBootMockMvcBuilderCustomizer;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@SpringBootApplication
@Configuration
@ComponentScan("com.boot.demo")
public class Springboot02Application extends SpringBootServletInitializer{
	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder){
		return builder.sources(Springboot02Application.class);
	}

}

步骤四:
打包。

比如在eclipse中,
可以Run As  ------>   Maven build... ------>(在goals中填写 clean packge)------>run
然后控制台中会显示结果以及war的路径。

注: 项目是不需要web.xml的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值