实现
首先设置打包的方式,这里设置为war包。
找到pom.xml

修改打包时的项目依赖的tomcat,使其在编译运行时使用tomcat,打包时不需要将tomcat打进包中
<!-- 打包时依赖 provided:表示编译运行时使用tomcat,打包时不需要将tomcat打进包中-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
找到项目的启动类,使其继承SpringBootServletInitializer并重写方法configure来指定项目启动类。

package com.example.demo;
import java.util.List;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
@MapperScan("com.example.demo.mapper")
@EnableScheduling
@SpringBootApplication(scanBasePackages= {"com.example.demo.controller","com.example.demo.service","com.example.demo.interceptor","com.example.demo.handler","com.example.demo.job","com.example.demo.email"})
public class HelloSpringBootApplication extends SpringBootServletInitializer{
{
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
//指明启动类
return builder.sources(HelloSpringBootApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(HelloSpringBootApplication.class, args);
}
}
右键项目--run as --Maven build

输入命令 clean package,然后点击Run

打包成功

来到项目的target目录下,可以看到打包后的war包。

如果打包时出现提示:
Perhaps you are running on a JRE rather than a JDK?