springboot 多模块打包war

1、配置项目pom.xml打包方式为war包,默认为jar包

<packaging>war</packaging>

2、设置主程序

<plugins>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.2.4.RELEASE</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <!--设置程序执行的主类-->
                    <mainClass>com.dr.archive.common.Application</mainClass>
                </configuration>

            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
            </plugin>
        </plugins>

3、配置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>
            <!--打包的时候可以不用包进去,别的设施会提供。事实上该依赖理论上可以参与编译,测试,运行等周期。
                相当于compile,但是打包阶段做了exclude操作-->
            <scope>provided</scope>
        </dependency>

4、启动类继承 SpringBootServletInitializer,重写configure方法

1、Application.java

 @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(Application.class);
    }

5、注意项目访问需要把静态页面放到tomcat web app 下的root中,并且项目访问需要加上项目名称(webapp下的文件名称)

6、修改自己代码中前台访问后台的页面地址 加上项目名端口等

注意打包时,不执行测试代码

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>3.1.2</version>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <!--打包的时候不执行测试-->
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.2</version>
            <configuration>
                <skipTests>true</skipTests>
            </configuration>
        </plugin>
    </plugins>
</build>

附完整代码
1、Application.java

package com.dr.archive.common;

import com.dr.archive.common.config.ArchiveConfig;
import com.dr.framework.core.orm.support.mybatis.spring.boot.autoconfigure.EnableAutoMapper;
import com.dr.framework.util.Constants;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;


/**
 * 系统启动入口
 *
 * @author dr
 */
@SpringBootApplication(scanBasePackages = {Constants.PACKAGE_NAME, "com.dr.archive"}, scanBasePackageClasses = Application.class)
@EnableConfigurationProperties(ArchiveConfig.class)
@EnableAutoMapper(basePackages = {Constants.PACKAGE_NAME, "com.dr.archive"})
@EnableScheduling
public class Application extends SpringBootServletInitializer {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
        System.out.println("成功");
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(Application.class);
    }

    /**
     * httpClient
     *
     * @return
     */
    @Bean
    public RestTemplate restTemplate() {
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setReadTimeout(10000);
        // 设置超时
        requestFactory.setConnectTimeout(10000);
        //利用复杂构造器可以实现超时设置,内部实际实现为 HttpClient
        RestTemplate restTemplate = new RestTemplate(requestFactory);
        for (HttpMessageConverter messageConverter : restTemplate.getMessageConverters()) {
            if (messageConverter instanceof StringHttpMessageConverter) {
                ((StringHttpMessageConverter) messageConverter).setDefaultCharset(StandardCharsets.UTF_8);
            }
        }
        return restTemplate;
    }
}
2、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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.dr.archive</groupId>
        <artifactId>parent</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
    <artifactId>archive_commons</artifactId>
    <packaging>war</packaging>
    <repositories>
        <repository>
            <id>com.e-iceblue</id>
            <name>e-iceblue</name>
            <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
        </repository>
    </repositories>
    <dependencies>
        <!--档案核心相关开始-->
        <dependency>
            <groupId>com.dr.archive</groupId>
            <artifactId>core</artifactId>
        </dependency>
        <dependency>
            <groupId>com.dr.archive</groupId>
            <artifactId>detection</artifactId>
        </dependency>
        <dependency>
            <groupId>com.dr.archive</groupId>
            <artifactId>storage</artifactId>
        </dependency>
        <dependency>
            <groupId>com.dr.archive</groupId>
            <artifactId>kufang</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>2.0.1.Final</version>
        </dependency>
        <!--档案核心相关结束-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.bouncycastle</groupId>
            <artifactId>bcprov-jdk15on</artifactId>
            <version>1.47</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.9.3</version>
        </dependency>
        <dependency>
            <groupId>com.dr.archive.fuzhou</groupId>
            <artifactId>api</artifactId>
        </dependency>
        <!--pdf相关开始-->
        <dependency>
            <groupId>org.jodconverter</groupId>
            <artifactId>jodconverter-local</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jodconverter</groupId>
            <artifactId>jodconverter-spring</artifactId>
        </dependency>
        <dependency>
            <groupId>org.ofdrw</groupId>
            <artifactId>ofdrw-full</artifactId>
            <version>1.7.1</version>
            <type>pom</type>
            <exclusions>
                <exclusion>
                    <groupId>org.ofdrw</groupId>
                    <artifactId>ofdrw-converter</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>net.coobird</groupId>
            <artifactId>thumbnailator</artifactId>
            <version>[0.4, 0.5)</version>
        </dependency>
        <!--excel模板开始-->
        <dependency>
            <groupId>org.jxls</groupId>
            <artifactId>jxls</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jxls</groupId>
            <artifactId>jxls-poi</artifactId>
        </dependency>
        <!--excel模板结束-->
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--这个是先将word另存为xml,再根据xml嵌套数据生成word-->
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.30</version>
        </dependency>
        <!--这个可以直接根据word模板生成新的word-->
        <dependency>
            <groupId>com.deepoove</groupId>
            <artifactId>poi-tl</artifactId>
            <version>1.9.0</version>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.4.3</version>
            <scope>compile</scope>
        </dependency>

        <!-- websocket -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
        </dependency>


        <!--导出word-->
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-base</artifactId>
            <version>3.0.3</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-web</artifactId>
            <version>3.0.3</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-annotation</artifactId>
            <version>3.0.3</version>
        </dependency>

        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>core</artifactId>
            <version>3.3.3</version>
        </dependency>
        <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>
            <!--打包的时候可以不用包进去,别的设施会提供。事实上该依赖理论上可以参与编译,测试,运行等周期。
                相当于compile,但是打包阶段做了exclude操作-->
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <!--包含resources文件夹以及子文件夹下所有资源-->
                    <include>**/*.*</include>
                </includes>
            </resource>
            <resource>
                <directory>${basedir}/../web/dist</directory>
                <targetPath>static</targetPath>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.2.4.RELEASE</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <!--设置程序执行的主类-->
                    <mainClass>com.dr.archive.common.Application</mainClass>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

3、http.js(自己项目用的,别人没用)

import axios from 'axios'
import NProgress from "nprogress";
import 'nprogress/nprogress.css';
import qs from 'qs'
import util from '../components/login/util'
import {Message} from "element-ui";

/**
 * 默认配置项
 */
const defaultCfg = {
    baseURL: 'archive_commons-1.0-SNAPSHOT/api', timeout: 5000,
    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
    transformRequest(data) {
        NProgress.start()
        return qs.stringify(data)
    },
    transformResponse(data) {
        NProgress.done()
        try {
            data = JSON.parse(data)
        } catch (e) {
            data = {success: false, message: '数据格式不正确'}
        }
        return data
    }
}
let httpInstance;

/**
 * 初始化默认http客户端
 * @param base
 * @returns {AxiosInstance}
 */
export function initHttp(cfg = defaultCfg) {
    httpInstance = axios.create(cfg)
    return httpInstance;
}

/**
 * 获取默认的http客户端
 * @returns {*}
 */
export function http() {
    if (!httpInstance) {
        initHttp()
    }
    return httpInstance;
}

/**
 * 这里会被扫描到,用来初始化http请求
 * @param vue
 * @param router
 */
export default (vue, router, store) => {
    const instance = http()
    //拦截所有的请求,添加token
    instance.interceptors.request.use(config => {
        const header = config.headers
        header['$token'] = header['dauth'] = util.getToken()
        return config;
    }, err => {
        return Promise.reject(err);
    })
    instance.interceptors.response.use(
        response => {
            if (response.data.message === 'needLogin') {
                router.replace({path: 'login', query: {redirect: router.currentRoute.fullPath}})
            } else {
                return response
            }
        },
        err => {
            //routeLogin(options)
            let message = `服务器错误【${err}】`
            if (err && err.message.startsWith('timeout')) {
                message = `网络请求超时,请稍候重试!`
            }
            Message.error(message)
            return Promise.resolve({data: {success: false, message}})
        })
    store.$http = vue.prototype.$http = instance
    store.$post = vue.prototype.$post = instance.post
    store.$get = vue.prototype.$get = instance.get
    store.$router = router

    //拦截跳转,添加加载进度
    router.beforeEach((route, redirect, next) => {
        NProgress.start()
        next()
    })
    router.afterEach(() => {
        NProgress.done()
    })
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

【这个世界会好的】

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值