花了半天时间终于成功,记录以备查阅。

一、第三方Tomcat部署

部署部分参考的是:把spring-boot项目部署到tomcat容器中

目标:把spring-boot项目按照平常的web项目一样发布到tomcat容器下

1. 修改打包形式

在pom.xml里设置 <packaging>war</packaging>

<groupId>com.study</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

2. 移除嵌入式tomcat插件

在pom.xml里找到spring-boot-starter-web依赖节点,在其中添加如下代码:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 移除嵌入式tomcat插件 -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>

3. 添加servlet-api的依赖

下面两种方式都可以,任选其一

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

4. 修改启动类,并重写初始化方法

我们平常用main方法启动的方式,都有一个App的启动类,代码如下:

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

直接让启动类继承SpringBootServletInitializer,并覆盖configure()方法:

@SpringBootApplication
public class Application extends SpringBootServletInitializer { @Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
// 注意这里要指向原先用main方法执行的Application启动类
return builder.sources(Application.class);
} public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

5. 打包部署

在项目根目录下(即包含pom.xml的目录),在命令行里输入:
mvn clean package即可, 等待打包完成,出现[INFO] BUILD SUCCESS即为打包成功。
mvn clean package 命令可以按需要添加参数,
-DskipTests 不执行测试用例,但编译测试用例类生成相应的class文件至target/test-classes下。
-Dspring.profiles.active=dev 如果配置了多环境,可以设置打包到哪个环境。
然后把target目录下的war包放到tomcat的webapps目录下,启动tomcat,即可自动解压部署。
最后在浏览器中输入 http://localhost:[端口号]/[打包项目名]/ 发布成功

二、IntelliJ IDEA下使用第三方Tomcat运行项目

1. 完成部署中的1,2,3,4步骤

2. 配置第三方Tomcat

IDEA上方工具栏:Run->Edit Configurations

打开配置界面,左边"+"号->Tomcat Server->Local

完成后如下所示

然后在Deployment中点击"+"号,选择client:war,这样每次server启动的时候都会去打包一次war包(个人理解),然后去运行war包。

然后保存就可以了,然后启动项目就运行起来了。

三、部署中遇到的问题

1. log4j.properties中使用了环境变量

log4j.appender.dailyFile.File=${catalina.base}/logs/guandata/log.log4j

在启动第三方Tomcat时,Tomcat会去环境变量中寻找叫CATALINA_HOME、CATALINA_BASE的环境变量,并将它们加入到Tomcat到系统变量中,这样我们就能在项目中使用它们了。

2. 部署后访问静态资源,访问路径URL中多了项目名,导致静态资源找不到

举例:

原本在springboot内置Tomcat中运行项目,资源访问路径URL类似于:http://localhost:8080/css/bootstrap.min.css
但是打包部署到第三方Tomcat中,Tomcat默认会在路径中加上项目名,类似于:http://localhost:8080/myproject/css/bootstrap.min.css
这样就导致了访问不到静态资源 404

原因:

由于在前端引用资源时使用了绝对路径:

<link href="/css/bootstrap.min.css" rel="stylesheet">

这样默认在 http://localhost:8080/css/bootstrap.min.css 下面寻找,因此,我们需要在前面加上项目名,但是写死的话万一改项目名就很麻烦,当然是有办法解决的。

解决:

1. 在Java后端定义一个拦截器,用来获取 request.getContextPath() 然后传到前端,request.getContextPath() 得到的是项目的根路径,具体参考:关于request.getServletPath(),request.getContextPath()的总结

 @Component
public class CommonIntercepter implements HandlerInterceptor { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
return true;
} @Override
public void postHandle(HttpServletRequest request,
HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
request.setAttribute("ctx", request.getContextPath());
request.setAttribute("version", DateTimeUtils.currentTimeMillis());
} @Override
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex)
throws Exception { } }

2. 前端在静态资源访问url前加上${ctx!},注意我用的是freemarker模版

<link href="${ctx!}/css/bootstrap.min.css" rel="stylesheet">
<link href="${ctx!}/css/font-awesome.min.css" rel="stylesheet">
<link href="${ctx!}/css/plugins/bootstrap-table/bootstrap-table.min.css" rel="stylesheet">
<link href="${ctx!}/css/animate.css" rel="stylesheet">
<link href="${ctx!}/css/style.css" rel="stylesheet">

这样就能正确访问到静态资源啦。

3. 要注意给Tomcat/bin路径下的脚本权限,否则可能会出现不可预料的后果

sudo chmod 777 /Library/Tomcat/apache-tomcat-9.0.10/bin/*.sh

IntelliJ IDEA SpringBoot 使用第三方Tomcat以及部署的更多相关文章

  1. SpringBoot以war包形式部署到外部Tomcat

    SpringBoot 项目打包时能打成 .jar 与 .war包文件,.jar使用 java -jar xx.jar 就可以启动,而 .war 可以部署到tomcat的 webapps 中,随tomc ...

  2. springboot打war包后部署到tomcat后访问返回404错误

    springboot打war包后部署到tomcat后访问返回404错误 1.正常情况下,修改打包方式为war <packaging>war</packaging> 2.启动类继 ...

  3. IntelliJ IDEA配置Tomcat及部署项目

    IntelliJ IDEA配置Tomcat及部署项目(原链接) 主要有以下几个要点 1.选择本地的tomcat容器. 2.可以选择修改访问路径. 3.On Update action 当我们按 Ctr ...

  4. Intellij IDEA 创建Web项目并在Tomcat中部署运行(不使用maven)【转载】

    原文链接:http://www.thinksaas.cn/topics/0/350/350000.html 一.创建Web项目 1.File -> New Module,进入创建项目窗口 2.选 ...

  5. Intellij IDEA 创建Web项目并在Tomcat中部署运行

      一.创建Web项目 1.File -> New Module,进入创建项目窗口 2.选择Java类型,在 Module name 处输入项目名,点击Next 3.勾选 Web Applica ...

  6. IntelliJ IDEA 学习(二):Intellij IDEA 创建Web项目并在Tomcat中部署运行IDEA

    一.创建Web项目  1.File -> New Module,进入创建项目窗口 2.选择Java类型,在 Module name 处输入项目名,点击Next 3.勾选 Web Applicat ...

  7. IDEA/JRebel实现内部/外部/远程Tomcat热部署Spring Boot

    1 概述 所谓热部署,对于Java应用程序来说,就是在运行时更新Java类文件.IDEA可以使用自带的Spring Boot热部署的方式进行本地/远程热部署,或者使用JRebel进行本地/远程热部署, ...

  8. SpringBoot入门教程(二)CentOS部署SpringBoot项目从0到1

    在之前的博文<详解intellij idea搭建SpringBoot>介绍了idea搭建SpringBoot的详细过程, 并在<CentOS安装Tomcat>中介绍了Tomca ...

  9. SpringBoot启动方式讲解和部署war项目到tomcat9

    1.SpringBoot启动方式讲解和部署war项目到tomcat9简介:SpringBoot常见启动方式讲解和部署war项目Tomcat 1.ide启动 2.jar包方式启动 maven插件: &l ...

随机推荐

  1. UVA.297 Quadtrees (四分树 DFS)

    UVA.297 Quadtrees (四分树 DFS) 题意分析 将一个正方形像素分成4个小的正方形,接着根据字符序列来判断是否继续分成小的正方形表示像素块.字符表示规则是: p表示这个像素块继续分解 ...

  2. python3.5安装pycrypto

    在python中使用AES加密是一种有效的加密方式,如果你研究过微信公众号api就会发现,它也用的是这个加密的.在写代码的时候,要安装crypto模块,在linux或者mac上都好说,但是在windo ...

  3. JavaScript关键字return的用法

    return 语句从当前函数退出,并从那个函数返回一个值. 语法: 1 return[()[expression][]]; 可选项 expression 参数是要从函数返回的值.如果省略,则该函数不返 ...

  4. HDU 5646

    DZY Loves Partition Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Ot ...

  5. Codeforces Round #341 (Div. 2)B

    B. Wet Shark and Bishops time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  6. caffe环境的搭建(Ubuntu14.04 64bit,无CUDA,caffe在CPU下运行)

    1. 安装BLAS : $ sudo apt-get install libatlas-base-dev 2. 安装依赖项: $ sudo apt-get install libprotobuf-de ...

  7. [linux]ubuntu限速软件

    wondersharper 1 安装wondershaper:sudo apt-get install wondershaper2 限制下载,上传速度(1500是限制下载速度(实际限速150k左右), ...

  8. SDUT 3930 线段树

    皮卡丘的梦想2 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 一天,一只住在 501 实验 ...

  9. SpringMVC 用注解Annotation驱动的IoC功能@Autowired @Component

    转载自:http://blog.csdn.net/lufeng20/article/details/7598564 本文分为三个部分:概述.使用注解进行属性注入.使用注解进行Bean的自动定义. 一, ...

  10. [C#] 小记 new 和 override 关键字

    C#要想实现函数的override,要求和C++一样,父类的函数必须用virtual关键字注明,随后在子类中用override关键字表明重写的函数. 子类同名函数定义时,如果什么都不写,或者使用new ...