springboot打包war形式,并外部tomcat运行

132 阅读1分钟

1.新建springboot项目,创建启动类;

2.配置pom.xml

<packaging>war</packaging>

3.配置依赖

<!-- 打包时去掉内置tomcat-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
</dependency>

4.配置打包依赖

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <fork>true</fork>
                <jvmArguments>Dfile.encoding=UTF-8</jvmArguments>
                <!--配置springboot入口类 此处必须配置-->
                <mainClass>com.war.demo.WarDemo</mainClass>
            </configuration>
        </plugin>
    </plugins>
</build>

5.如打包报错需要配置web.xml,则需要在项目 main目录下创建webapp,webapp目录下创建web.xml

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

6.将打好的war复制到tomcat 中webapps目录下,启动tomcat即可;