springboot打war包时将外部配置文件打入到war包内

需求

客户适配信创改造,想把war包外的配置文件都放入到war内,方便迁移war和部署

使用maven-assembly-plugins插件

打包代码改造如下。在项目的pom.xml文件中加入maven-assembly-plugins插件如下所示

XML 复制代码
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.1.0</version>
    <executions>
        <execution>
            <id>private-bhbank</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
            <configuration>
                <descriptors>
                    <descriptor>src/main/resources/xxx.xml</descriptor>
                </descriptors>
                <finalName>api</finalName>
            </configuration>
        </execution>
    </executions>
</plugin>

然后在Resources目录下新建xxx.xml文件,内容如下所示

XML 复制代码
<assembly
        xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
    <id>private-bhbank</id>
    <includeBaseDirectory>false</includeBaseDirectory>
    <formats>
        <format>war</format>
    </formats>
    <fileSets>
        <!--将目录复制到指定位置-->
        <fileSet>
            <directory>target/classes/</directory>
            <outputDirectory>WEB-INF/classes</outputDirectory>
        </fileSet>
        <!--将单文件复制到指定位置-->
        <fileSet>
            <directory>config/</directory>
            <excludes>
                <exclude>application.properties</exclude>
            </excludes>
            <outputDirectory>WEB-INF/classes/config</outputDirectory>
        </fileSet>
        <!--将多文件复制到指定位置-->
        <fileSet>
            <directory>customerConfig/config-xxx</directory>
            <includes>
                <include>config.properties</include>
                <include>application.properties</include>
            </includes>
            <outputDirectory>WEB-INF/classes/config</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>customerConfig/config-xxx</directory>
            <includes>
                <include>log4j2.xml</include>
            </includes>
            <outputDirectory>WEB-INF/classes</outputDirectory>
        </fileSet>
    </fileSets>
    <dependencySets>
        <dependencySet>
            <useProjectArtifact>false</useProjectArtifact>
            <outputDirectory>WEB-INF/lib</outputDirectory><!-- 将scope为runtime的依赖包打包到lib目录下。 -->
            <excludes>
                  <!--排除某些包-->
                <exclude>${包的groupId}:${包的artifactId}</exclude>
            </excludes>
        </dependencySet>
    </dependencySets>
</assembly>

按照上面改造后就可以在打包时将项目根目录的指定文件或目录复制到war包内的指定位置

相关推荐
文丑颜不良啊2 小时前
LeetCode3281~3300题目解析
java·leetcode
用户46043852250862 小时前
关于 Paimon-Rust 的写操作(细节)
后端
掘金酱3 小时前
AI 带来的新生意|稀土掘金 X-Lab 年度大会招募开启
前端·后端·vibecoding
nice_lcj5203 小时前
【从零理解计算机网络 · 02】分层模型:别背 OSI 七层,看数据怎么被“打包“和“拆包“
java·后端·tcp/ip·计算机网络·面试·osi·分层模型
SimonKing4 小时前
给Deepseek Harness装上插件市场,从此插件无忧
java·后端·程序员
艾莉丝努力练剑4 小时前
【AI大模型接入SDK】C++的SessionManager 线程安全
java·开发语言·前端·c++·人工智能·学习·安全
aiotto资源分享5 小时前
Python 写 JSON 文件,程序中途退出会留下什么?
java·python·json·jsondecodeerror
利威尔xu5 小时前
Go Mutex 饥饿模式:正常模式等待者饿死,1ms 阈值自动切换崩溃
开发语言·后端·面试·golang·并发编程
一条Go5 小时前
边缘 Agent:为什么是它连出来,而不是平台连进去
后端·架构·github
RuoyiOffice5 小时前
SpringBoot3+Vue3 采购到付款:ERP、WMS与财务如何按同一来源协同
java·spring boot·架构·vue