美文网首页
spring boot部署排坑,同时支持内嵌与外部两种tomca

spring boot部署排坑,同时支持内嵌与外部两种tomca

作者: NewBornCyanide | 来源:发表于2019-10-25 10:53 被阅读0次

springboot版本2.1.6
照着百度搜到的教程还是没部署成功war的朋友可以看这里

1.改启动类:

加configue方法,与常见的百度答案相同;

2.pom文件:

2.1

首先需要加

<packaging>war</packaging>,

2.2

build下的plugins节点加入如下plugin

<plugin>
         <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
                    <mainClass>你的启动类</mainClass>
         </configuration>
</plugin>

2.3

最好自己在build中设置finalName,

2.4

并不像某些答案中说的需要在application.properties中设置server.servlet.context-path相同的值,因为无论是server.servlet.context-path还是server.port都是针对springboot内嵌的tomcat而言的;

2.5

实际上不需要在spring-boot-starter-web中使用exclusion排除tomcat,更不需要加

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>

2.6

如果使用jsp需要加上

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
</dependency>

否则路径敲对会报jsp的404;

2.7

build节点中还要加上(没测试过是不是必须加,欢迎反馈

<resources>
     <resource>
          <directory>src/main/resources</directory>
          <includes>
                <include>**/**</include>
            </includes>
          <filtering>false</filtering>
    </resource>
</resources>

3.在web-inf下加入web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                      http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        version="2.5">
</web-app>

4.其他问题:

4.1

关于resources/static下的静态资源访问不到的问题,静态资源路径配置如下

spring.resources.static-locations=classpath:/static
spring.mvc.static-path-pattern=/**

对应的存储位置是resource/static。前端页面(是放在了resource/template下,webapp的情况没测试过)的路径如下

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

4.2

关于自己在项目中创建的文件夹在war中找不到的问题,pom加入这个plugin,

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<includeEmptyDirectories>true</includeEmptyDirectories>
</configuration>
</plugin>

打包命令:mvn clean package
在外部tomcat中运行:到target下找到war包丢进tomcat的webapp里面,启动,运行,路径是你的war名/主页面路径,端口是当前tomcat的端口
在内嵌tomcat中运行:run as springboot app,端口是application.properties配置的,路径是server.servlet.context-path配置的/主页面路径

相关文章

网友评论

      本文标题:spring boot部署排坑,同时支持内嵌与外部两种tomca

      本文链接:https://www.haomeiwen.com/subject/eaqsvctx.html