springboot项目通过maven的profile功能实现通过不同文件夹的方式来组织不同环境配置文件

写在前面

本文看下springboot项目如何通过文件夹的方式来组织不同环境配置文件。

1:正文

一般的我们写springboot项目时配置文件是这个样子的:

复制代码
appliction.yaml --> 通过spring.profiles.active=xxx来激活某个指定后缀的配置文件
application-evn1.yml
application-evn2.yml
application-evn3.yml

上面不同环境的配置放在了不同的配置文件中。接着,随着项目的增大,越来越多的功能和中间件会被引入,自然的每个文件配置的内容会越来越多,这个时候为了方便维护我们会通过拆分成多个文件的方式来解决这个问题,此时就变成了这样:

复制代码
appliction.yaml --> 通过spring.profiles.active=xxx来激活某个指定后缀的配置文件

application-evn1-mq.yml
application-evn1-auth.yml
application-evn1-mysql.yml

application-evn2-mq.yml
application-evn2-auth.yml
application-evn2-mysql.yml


application-evn3-mq.yml
application-evn3-auth.yml
application-evn3-mysql.yml

这个时候包括主配置文件在内我们就有了10个配置文件在一起堆着,当然实际的情况配置文件的数量要比这个还要多,要改一个配置找都得找半天啦!如何解决这个问题?可以考虑再加一层文件夹,每个文件夹放一个环境的配置文件,这样就能很快定位到具体的环境,进而找到对应的配置文件进行修改,也就是变成这样子:

复制代码
appliction.yaml --> 通过spring.profiles.active=xxx来激活某个指定后缀的配置文件

evn1(文件夹):
    application-evn1-mq.yml
    application-evn1-auth.yml
    application-evn1-mysql.yml

evn2(文件夹):
	application-evn2-mq.yml
	application-evn2-auth.yml
	application-evn2-mysql.yml

evn3(文件夹):
    application-evn3-mq.yml
    application-evn3-auth.yml
    application-evn3-mysql.yml

想要实现这种效果,我们需要依赖于maven提供的profile来做,首先配置如下:

xml 复制代码
 <profiles>
    <profile>
        <!-- 本地开发环境 -->
        <id>xx</id>
        <properties>
            <profiles.active>xx</profiles.active>
            <maven.test.skip>true</maven.test.skip>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <profile>
        <!-- 本地开发环境 -->
        <id>dev</id>
        <properties>
            <profiles.active>dev</profiles.active>
            <maven.test.skip>true</maven.test.skip>
        </properties>
        <activation>
            <activeByDefault>false</activeByDefault>
        </activation>
    </profile>
</profiles>


<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>2.3.1.RELEASE</version>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>

    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <!-- 资源根目录排除各环境的配置,防止在生成目录中多余其它目录 -->
            <excludes>
                <exclude>xx/*</exclude>
                <exclude>dev/*</exclude>
            </excludes>
        </resource>
        <resource>
            <directory>src/main/resources/${profiles.active}</directory>
        </resource>
    </resources>
</build>

这其实就是maven的profile功能,配置后在maven的视图中就可以看到相关的profile了:

这我们勾选谁,就会加载哪个文件下的配置信息了,比如我们其中xx:

如果是在测试环境或者是生产环境,则就可以这样来做CICD,比如dev:

复制代码
D:\\programs\\mvn363\\apache-maven-3.6.3-bin\\apache-maven-3.6.3\\bin\\mvn clean package -Pdev

如下就是打出的jar包,是个fat jar,直接运行就可以了:

所以借助于idea工具,在本地开发的时候,只需要通过ui操作就可以切换不同的profile了,当然通过maven命令也是可以的。

写在后面

参考文章列表

SpringBoot不同环境加载不同配置文件

maven中profiles使用详解

相关推荐
sunnyday042614 小时前
Spring Cloud Alibaba Sentinel 流量控制与熔断降级实战指南
spring boot·sentinel·springcloud
Coder_Boy_1 天前
基于SpringAI的在线考试系统-0到1全流程研发:DDD、TDD与CICD协同实践
java·人工智能·spring boot·架构·ddd·tdd
卓怡学长1 天前
m115乐购游戏商城系统
java·前端·数据库·spring boot·spring·游戏
未若君雅裁1 天前
SpringAI基础入门
java·spring boot·ai
JingAi_jia9171 天前
SpringBoot Jackson 序列化
spring boot·jackson·数据序列化·serializer
qq_12498707531 天前
基于小程序中医食谱推荐系统的设计(源码+论文+部署+安装)
java·spring boot·后端·微信小程序·小程序·毕业设计·计算机毕业设计
Coder_Boy_1 天前
基于SpringAI的在线考试系统-阅卷评分模块时序图
java·人工智能·spring boot
Remember_9931 天前
【数据结构】深入理解优先级队列与堆:从原理到应用
java·数据结构·算法·spring·leetcode·maven·哈希算法
牧小七1 天前
SpringBoot使用validation
spring boot
汤姆yu1 天前
基于springboot的直播管理系统
java·spring boot·后端