# springboot 项目部署到外部 tomcat

最近在用 spring boot 弄了一个学习型的项目,学习一下 spring boot 怎样构建项目,spring boot 本身是内置 tomcat 的,如果想部署到外部 tomcat, 就要做一些改变。
# 1 默认打包方式是 jar 包,改成 war 包打包,在 pom.xml 里
| <packaging>war</packaging> | 
# 2 在 maven 里排除自带 tomcat 插件,有两种方法
第一种:
| <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 将项目的启动类 Application.java 继承 SpringBootServletInitializer 并重写 configure 方法
| @SpringBootApplication | |
| public class BootdoApplication extends SpringBootServletInitializer { | |
| public static void main(String[] args) { | |
| SpringApplication.run(BootdoApplication.class, args); | |
| System.out.println("ヾ(◍°∇°◍)ノ゙ bootdo启动成功 ヾ(◍°∇°◍)ノ゙\n" + | |
| " ______ _ ______ \n" + | |
| "|_ _ \\ / |_|_ _ `. \n" + | |
| " | |_) | .--. .--. `| |-' | | `. \\ .--. \n" + | |
| " | __'. / .'`\\ \\/ .'`\\ \\| | | | | |/ .'`\\ \\ \n" + | |
| " _| |__) || \\__. || \\__. || |, _| |_.' /| \\__. | \n" + | |
| "|_______/ '.__.' '.__.' \\__/|______.' '.__.' "); | |
|     } | |
| protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { | |
|         // 注意这里要指向原先用 main 方法执行的 Application 启动类 | |
| return builder.sources(BootdoApplication.class); | |
|     } | 
# 4 在 idea 里用 maven 打包
maven clean and install 就打包完成了,在 target 下就能找到 war 包
# 5 后来发现 war 包在本地跑没问题,部署到服务器上就无法启动了
查阅 spring boot 官方文档发现 spring boot 只支持 tomcat 8.5 以上版本,而服务器上的版本是 7.0.47
需要在 pom.xml 里指定低版本的 tomcat
| <properties> | |
| <tomcat.version>7.0.47</tomcat.version> | |
| </properties> | 
