Skip to content

把spring boot项目编译成war

gexiangdong edited this page Jun 13, 2018 · 3 revisions

修改启动类

改成继承org.springframework.boot.web.servlet.support.SpringBootServletInitializer, 例如:

package cn.devmgr.tutorial;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class Application extends SpringBootServletInitializer{

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}
}

修改pom文件

改packaging方式为war

    <packaging>war</packaging>

改依赖

	<dependency>
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-starter-data-rest</artifactId>
            <!-- 排除掉内嵌的tomcat,为了减少war体积,也为了避免和tomcat服务器冲突 -->
	    <exclusions>
	        <exclusion>
	            <groupId>org.springframework.boot</groupId>
	            <artifactId>spring-boot-starter-tomcat</artifactId>
	        </exclusion>
	    </exclusions>
	</dependency>

	<!-- 去掉了内嵌的tomcat后,需要增加servlet-api,否则编译会出错了 -->
	<dependency>
	    <groupId>javax.servlet</groupId>
	    <artifactId>javax.servlet-api</artifactId>
	    <scope>provided</scope>
	</dependency>

注意事项

spring boot 2.0项目需要在tomcat 8.5以上版本环境下运行