1.首先,将项目打包方式定义为War包形式,修改pom.xml文件:
<packaging>war</packaging>
2.修改项目启动方式,由于SpringBoot默认使用了内置的Tomcat服务器启动,部署到服务器时应该设置其以外部Tomcat服务器启动,修改pom.xml文件内容如下,两种方式二选一即可:
<!-- 第一种方式 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 第二种方式 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
3.将启动类修改为,主要在于继承SpringBootServletInitializer并且重写了configure方法:
@SpringBootApplication
public class SpringbootApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(SpringbootApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
}
4.点击“package”进行项目打包,完成后在Target目录下即可获得项目war包: