1.首先修改war包装方法
<packaging>war</packaging>
2.删除springboot内置tomcat方法1:使用exclusion标签排除web启动器自带的tomcat
注:使用此方法需要添加servlet依赖:
<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>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <scope>provided</scope> </dependency>
方法二:使用scope标签控制所依赖的使用范围,并将参数设置为provided
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency>
3.在build标签中设置包装文件名
<finalName>name</finalName>
4.修改启动类(启动类main方法打war包后不会执行)
@SpringBootApplication@MapperScan("com.ssq.mapper")@EnableSchedulingpublic class MyApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(MyApplication.class); }
5.在main方法中初始化一些业务之前,因为main方法不会在打war包后实施,所以建立一个新的配置类来实施业务初始化代码
@Componentpublic class BeginConfig implements ApplicationListener {@Overridepublic void onApplicationEvent(ContextRefreshedEvent event) {///在此编写项目启动后需要执行的初始代码}
6.将前端文件复制到resource目录下的static目录下7。在build标签下配置resource标签,确保将前端文件打到war包中
<resource> <directory>src/main/resources</directory> <includes> <include>**/*</include> </includes> </resource>
8.用maven插件包装,包装前最好是clean,然后部署在外部tomcat中即可访问