SpringBoot写的项目,自身嵌入了tomcat,所以可以直接运行jar包。但是,每次启动jar包创建的都是新的tomcat,这回造成上传文件丢失等问题。因此,我们需要将项目打成war包,部署到tomcat上。
<groupId>cn.bookcycle.pandagroupId>
<artifactId>panda-payserviceartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>jarpackaging>
修改为:
<groupId>cn.bookcycle.pandagroupId>
<artifactId>panda-payserviceartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>warpackaging>
2.在pom.xml中添加打war包的maven插件和设置打包的时候跳过单元测试代码
<plugin>
<artifactId>maven-war-pluginartifactId>
<configuration>
<failOnMissingWebXml>falsefailOnMissingWebXml>
<warName>checkroomwarName>
configuration>
plugin>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-surefire-pluginartifactId>
<configuration>
<skip>trueskip>
configuration>
plugin>
3.在pom.xml中添加servlet包
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
dependency>
4.排除Spring Boot内置的tomcat
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
修改为
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-tomcatartifactId>
exclusion>
exclusions>
dependency>
5.在main方法所属的类的同级包下,新建SpringBootStartApplication类
public class SpringBootStartApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
// 注意的Application是启动类,就是main方法所属的类
return builder.sources(Application.class);
}
6 通过eclipse开始打war包
项目右键——》Run As——》Maven Build…——》找到Goals栏,写命令clean package ——》Run
7 访问项目
最后,在项所在的文件夹文件夹里找target/*.war
将war包拷贝到Tomcat的webapps下,然后启动Tomcat
访问路径是:http://localhost:端口号/war包名/@RequestMapping中的value值
注意:
端口号是Tomcat的端口号,不是Spring Boot中配置的项目端口号
如果打包过程中遇到
[WARNING] The requested profile “pom.xml” could not be activated because it does not exist.
可以在打包的时候,清空Goals栏下面的Profiles栏的内容