一、概述

  springboot 带有内置Tomcat 服务器,可以直接将项目打包成jar运行,如果在需要把项目打成war包,使用外置tomcat部署。下面是将springboot项目部署为war项目的一些步骤。

二、Springboot程序打成war包

1、在pom.xml中将打包形式 jar 修改为war

    <packaging>war</packaging>

2、将springboot内置的Tomcat依赖移除,但是为了我们在本机测试方便,我们还需要引入它,所以配置如下:

原生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>

如果是thymeleaf 模板引擎依赖移除

        <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</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、如果jsp,servlet等需求,需添加tomcat-servelt-api依赖

    <dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-servlet-api</artifactId>
<version>7.0.42</version>
<scope>provided</scope>
</dependency>

4、修改入口方法 继承一个SpringBootServletInitializer类,并且覆盖configure方法

public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(application名称.class);
}
}

5、配置启动即可【可不用配置】

            <!-- Tomcat -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<!--设置访问路径-->
<path>/${project.artifactId}</path>
<!--访问端口-->
<port>8081</port>
</configuration>
</plugin>

注:<scope>provided</scope>表示在编译和测试时使用(不加它,打的包中会指定tomcat,用tomcat部署时会因tomcat版本报错;而加上它,打包时不会把内置的tomcat打进去)

还要注意:spring-boot项目使用的jdk版本要和tomcat的jdk版本一致(都是1.8);tomcat的lib中el-api.jar版本最好要是javax.el-api-3.0.0.jar版本,防止低版本冲突。

需要注意的是这样部署的request url需要在端口后加上项目的名字才能正常访问。

spring-boot更加强大的一点就是:即便项目是以上配置,依然可以用内嵌的tomcat来调试,启动命令和以前没变

三、maven中三种classpath

编译,测试,运行 
1.compile:默认范围,编译测试运行都有效 
2.provided:在编译和测试时有效 
3.runtime:在测试和运行时有效 
4.test:只在测试时有效 
5.system:在编译和测试时有效,与本机系统关联,可移植性差

四、问题

配置完上述,使用tomcat可以运行,但是直接运行main提示不可用缺少servlet等

网上答案,修改spring-boot-starter-tomcat  为compile,此时可以使用,但是打包时候,多了tomcat,再部署到tomcat容器时候出现问题。

参看解决方案:

  https://github.com/spring-projects/spring-boot/issues/5666:文章提示看以下文章

  https://stackoverflow.com/questions/32531422/spring-boot-jsp-error-noclassdeffounderror:提示以下几种解决方案

There's a bug in IntelliJ that means that provided dependencies aren't added to the classpath. Assuming you want to stick with IDEA, you have a few options:

Manually configure the classpath in IDEA
Run the samples on the command line using mvn spring-boot:run
Remove all occurrences of <scope>provided</scope> from the pom. This will mean that app can't be deployed as a war to Tomcat or similar
EDIT: The bug is fixed and the server will start normally, as long as you tick the Include dependencies with "Provided" scope checkbox in the run configuration, below classpath.

可以看到

方法一、在IDEA中手动配置类路径【推荐】

  在Edit Configurations中,找到自己的application项目use classpath of module,选中include dependcenies with "provided" scope,即可

  对于Spring-Boot项目默认是启用的,如果默认没启用所以报错

  旧版本的idea,没有此选项,新的版本已经修复了,可以在官网下载新版本IntelliJ IDEA 2018.1.4,默认是勾选Include dependencies with “Provided” scope的,这样就不用每次注释了!

方法二、使用mvn spring-boot:run在命令行上运行【推荐】

方式三、移除<scope>provided</scope>,但是不能再war包上使用。【不推荐】

使用spring-boot 2.0.4

        <dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency> <dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.1.Final</version>
</dependency>

002-Spring Boot将WAR文件部署到Tomcat的更多相关文章

  1. spring boot 打包war后 部署到外部 tomcat 的具体正确操作【包括修改端口 与 去除请求路径的工程名】

    1.前言 工程做好了,总不能放在idea运行吧?不然怎么把项目放到云服务器呢?[这一篇随笔不讲解发布的云服务器的操作,在其他随笔有详细记载.] 解决的方案是把springboot 工程 打包成war文 ...

  2. Spring Boot打包war jar 部署tomcat

    概述 1.Spring Boot聚合工程打包war部署Tomcat 2.Spring Boot打包Jar,通过Java -jar直接运行. 3.提供完整pom.xml测试项目 至github 4.项目 ...

  3. Spring Boot 以 war 方式部署

    Spring Boot 默认自带了一个嵌入式的 Tomcat 服务器,可以以jar方式运行,更为常见的情况是需要将 Spring Boot 应用打包成一个 war 包,部署到 Tomcat.Jerry ...

  4. 如何把kotlin+spring boot开发的项目部署在tomcat上

    本文只讲部署过程,你首先要保证你的程序能在IDE里跑起来: 先看看你的application.properties中设置的端口号与你服务器上tomcat的端口号是否一致 server.port=80 ...

  5. spring boot 打war包部署,打jar包

    官方文档:http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-create-a-deployable- ...

  6. spring boot 使用war包部署

  7. Spring Boot发布war包流程

    1.修改web model的pom.xml <packaging>war</packaging> SpringBoot默认发布的都是jar,因此要修改默认的打包方式jar为wa ...

  8. Spring Boot 2 构建可部署的war包

    默认情况下Spring Boot使用了内嵌的Tomcat服务器,项目最终被打成jar包运行,每个jar包可以被看作一个独立的Web服务器.传统的Web开发,一般会将Web应用打成一个war包,然后将其 ...

  9. Spring Boot(十二):spring boot如何测试打包部署

    Spring Boot(十二):spring boot如何测试打包部署 一.开发阶段 1,单元测试 在开发阶段的时候最重要的是单元测试了,springboot对单元测试的支持已经很完善了. (1)在p ...

随机推荐

  1. 查看、分析memcached使用状态

    访问量上升,数据库压力大,怎么办?好办法是在中间挡一层缓存!这个缓存要求高效,不能比数据库慢,否则服务质量受影响:如果能把数据用hash打散存储到硬盘,也是可以的,不过在内存越来越便宜的今天,还是使用 ...

  2. MyBatis增删改查模板

    1. 首先,和Spring整合一下 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=& ...

  3. 虚拟机和Docker的异同

    [摘要]各种虚拟机技术开启了云计算时代:而Docker,作为下一代虚拟化技术,正在改变我们开发.测试.部署应用的方式.那虚拟机与Docker究竟有何不同呢? 首先,大家需要明确一点,Docker容器不 ...

  4. Cocos2d-x 3.0final 终结者系列教程07-画图节点Node

    在Cocos2d-x中全部能看到的都是引擎调用底层图形库函数绘制完毕的. Cocos2d-x将屏幕全部要绘制的全部内容逻辑上保存到一个场景Scene中(尺寸通常会和屏幕大小一致) 而在Scene中又包 ...

  5. eclipse启动无响应,老是加载不了revert resources,或停留在Loading workbench状态

    做开发的同学们或多或少的都会遇到eclipse启动到一定程度时,就进入灰色无响应状态再也不动了.启动画面始终停留在Loading workbench状态.反复重启,状态依旧. 多数情况下,应该是非正常 ...

  6. 新型智能芯片nxp----嗯质朴

    公司omap 用到nxp的qx 操作系统   由飞利浦公司创立,已拥有五十年的悠久历史,主要提供工程师与设计人员各种半导体产品与软件,为移动通信.消费类电子.安全应用.非接触式付费与连线,以及车内娱乐 ...

  7. 手机游戏运营主要的指标是什么? 7天活跃, 14天活跃 ARPU ?如何提升游戏 app 的虚拟道具的收入?

    数据采集越细,手段越丰富,所获得的数据也就更加详实,虽然手机游戏没有网游那么复杂,但也需要数据化运营,而且是必要的,是优化游戏收入的关键,大家最主要关心的是下面三类数据的指标 1. 用户数量首先,在移 ...

  8. hdu 2717:Catch That Cow(bfs广搜,经典题,一维数组搜索)

    Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  9. Apache里的httpd-vhosts.conf详解

    首先看下面的配置: <VirtualHost *:80> ServerAdmin webmaster@dummy-host.example.com DocumentRoot "D ...

  10. windows 2003 发布遇到问题---分析器错误消息: 未能加载类型“YWPT.MvcApplication”。

    问题如下: “/”应用程序中的服务器错误. ------------------------------------------------------------------------------ ...