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的