1.将pom.xml中的打包方式修改为war
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
2、将springboot自带的spring-boot-starter-web包中的spring-boot-starter-tomcat去掉,改成spring-boot-starter-tomcat,
即:将
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
改成:
<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、增加一个启动类,保持原有的启动类不变,这样不影响项目开发,开发时使用原来的启动类,打包用新增的这个启动类,代码如下:
public class DeploymentApplicationextends SpringBootServletInitializer implements WebApplicationInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application){
return application.sources(DemoApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
4、将项目重新构建一下
Build --> Build Project
5、打开Maven窗口,展开项目的Lifecycle目录,双击package,控制台输出以下内容,则打包成功:
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
6、将打包好的项目发布到Tomcat服务器上
从项目的target目录下找到打包好的war包,将其拷贝到Tomcat的webapps目录下,启动Tomcat服务就可。