Spring Boot项目打包成war包
如果在我们的项目中,需要将项目打包为war包,需要在pom.xml文件中,将打包方式改为war:
<packaging>war</packaging>
然后添加如下的Tomcat依赖配置,覆盖Spring Boot自带的Tomcat依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
在标签内配置项目名(该配置类似于server.context-path=allan):
...
<build>
...
<finalName>allan</finalName>
</build>
...
添加启动类ServletInitializer:
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}
其中Application为Spring Boot的启动类。
准备完毕后,运行mvn clean package命令即可在target目录下产生war包: