淡语

导航

springboot多模块项目,打包成war方式,部署到Tomcat

在springboot项目搭建完成之后,通过Tomcat方式进行部署项目,需进行一下操作

一.DanyuWebApplication入口启动类改造

 添加继承关系和Override重写,具体如下:

package com.danyu.web;

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

/**
 * web容器中进行部署
 *
 * @author danyu
 */
@SpringBootApplication()
public class DanyuWebApplication extends SpringBootServletInitializer
{
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application)
    {
        return application.sources(DanyuWebApplication.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(DanyuWebApplication.class, args);
        System.out.println("系统启动成功");
    }

}

 二。resource目录下添加transaction.xml文件

<!-- 解决读不到配置文件的问题,将指定的文件打进war包 -->
<resources>
    <resource>
        <!-- 要打进war包的文件所在的目录 -->
        <directory>src/main/resorce</directory>
        <includes>
            <include>**.*</include>
            <include>**/*.*</include>
            <include>**/*/*.*</include>
        </includes>
        <filtering>true</filtering>
    </resource>
</resources>

 三.修改子模块(danyu-web)pom.xml文件

3.1.添加<packaging>war</packaging>,将打包方式改为war

3.2 spring-boot-starter-web忽略内嵌tomcat

 

     <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <!--忽略内嵌tomcat,打包部署到tomcat。注*本地运行的时候要把这一段忽略引入个注释掉,要不然项目启动不了 -->
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

 

 3.3 添加spring-boot-starter-tomcat

<!-- 这个依赖让你能够在程序入口类:xxxAppAplication中直接执行main方法启动tomcat -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <!-- 但是这里一定要设置为provided -->
            <scope>provided</scope>
        </dependency>

 3.4 依赖其他子模块的需注意,添加<type>jar</type>

    <!-- 通用工具-->
        <dependency>
            <groupId>com.zbbz</groupId>
            <artifactId>zbbzfw-common</artifactId>
            <version>1.0.1</version>
            <!-- 该模块的packaging为war,所以下面两项必须设置,否则打包会出出错  -->
            <type>jar</type>
            <!--            <classifier>classes</classifier>-->
        </dependency>    

 3.5 打包配置

<build>
        <finalName>danyu</finalName>
        <plugins>
            <!-- war包插件 -->
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <!-- 把class打包jar作为附件 -->
                    <attachClasses>true</attachClasses>
                </configuration>
            </plugin>
            <!-- 指定启动入口 -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.danyu.web.DanyuWebApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>

    </build>

 整个pom.xml完整配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>danyu</artifactId>
        <groupId>com.danyu</groupId>
        <version>0.0.1</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>danyu-web</artifactId>
    <description>控制模块</description>
    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <!--忽略内嵌tomcat,打包部署到tomcat。注*本地运行的时候要把这一段忽略引入个注释掉,要不然项目启动不了 -->
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- 这个依赖让你能够在程序入口类:xxxAppAplication中直接执行main方法启动tomcat -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <!-- 但是这里一定要设置为provided -->
            <scope>provided</scope>
        </dependency>

        <!-- 通用工具-->
        <dependency>
            <groupId>com.zbbz</groupId>
            <artifactId>zbbzfw-common</artifactId>
            <version>1.0.1</version>
            <!-- 该模块的packaging为war,所以下面两项必须设置,否则打包会出出错  -->
            <type>jar</type>
            <!--            <classifier>classes</classifier>-->
        </dependency>

    </dependencies>

    <build>
        <finalName>danyu</finalName>
        <plugins>
            <!-- war包插件 -->
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <!-- 把class打包jar作为附件 -->
                    <attachClasses>true</attachClasses>
                </configuration>
            </plugin>
            <!-- 指定启动入口 -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.danyu.web.DanyuWebApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>

    </build>

</project>

 

posted on 2021-01-11 16:56  object360  阅读(1808)  评论(0编辑  收藏  举报