1.需要在pom.xml文件中添加如下内容
指定将当前应用打成war包
<!-- 指定将当前应用打成war包 -->
<packaging>war</packaging>
2.指定打包的项目名
<finalName>demo</finalName>
<build>
<!-- 指定打包的项目名 -->
<finalName>demo</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
3.去除内置tomcat容器,添加servlet-api依赖
<!--使用Spring MVC构建Web(包括RESTful)应用程序的入门者。使用Tomcat作为默认的嵌入式容器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 去除内置tomcat容器 -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 添加servlet-api的依赖,用来打war包 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
4.改造启动类,继承SpringBootServletInitializer类重写configure方法
@SpringBootApplication
public class Demo2Application extends SpringBootServletInitializer{
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(Demo2Application.class);
}
public static void main(String[] args) {
SpringApplication.run(Demo2Application.class);
}
}