一、说明
- 本文接上一篇内容,在上一篇基础上完成部署到Tomcat的过程
- 本文学习资源来自SpringBoot官网
二、 代码修改
1. 准备好上节完成的代码
![Springboot2.0学习3 项目部署到tomcat](http://p2.ttnews.xyz/loading.gif)
2. 把启动类从SpringBootServerletInitializer派生,用来支持WAR部署
Application.java修改后如下:
<code>package hello;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.builder.SpringApplicationBuilder;import org.springframework.boot.web.support.SpringBootServletInitializer;@SpringBootApplicationpublic class Application extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(Application.class); } public static void main(String[] args) { SpringApplication.run(Application.class, args); }}/<code>
3. 设置主类
gradle
在build.gralde里添加:
<code>mainClassName="hello.Application"/<code>
maven
在pom.xml里添加:
<code><start-class>hello.Application/<start-class>/<code>
4. 打包支持WAR
gradle build.gradle
<code>apply plugin: 'war'apply plugin: 'application'/<code>
maven pom.xml
<code><packaging>war/<packaging>/<code>
修改后的build.gradle
<code>buildscript { ext { springBootVersion = '1.5.8.RELEASE' } repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") }}apply plugin: 'java'apply plugin: 'eclipse'apply plugin: 'org.springframework.boot'apply plugin: 'war'apply plugin: 'application'group 'com.test'version '1.0-SNAPSHOT'sourceCompatibility = 1.8mainClassName="hello.Application"repositories { mavenCentral()}dependencies { compile('org.springframework.boot:spring-boot-starter-web') testCompile('org.springframework.boot:spring-boot-starter-test')}/<code>
三、打包
maven
<code>mvn package/<code>
gradle
<code>gradle clean build/<code>
可以看到build/libs里生成了war包
四、部署
可以通过tomcat manager来部署:
![Springboot2.0学习3 项目部署到tomcat](http://p2.ttnews.xyz/loading.gif)
或者直接拷到tomcat的webapps下。
閱讀更多 編程圈 的文章