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使用详解

相关推荐
昵称为空C12 小时前
SpringBoot3 http接口调用新方式RestClient + @HttpExchange像使用Feign一样调用
spring boot·后端
zjjuejin14 小时前
Maven环境搭建
后端·maven
麦兜*21 小时前
MongoDB Atlas 云数据库实战:从零搭建全球多节点集群
java·数据库·spring boot·mongodb·spring·spring cloud
麦兜*21 小时前
MongoDB 在物联网(IoT)中的应用:海量时序数据处理方案
java·数据库·spring boot·物联网·mongodb·spring
汤姆yu21 小时前
基于springboot的毕业旅游一站式定制系统
spring boot·后端·旅游
计算机毕业设计木哥1 天前
计算机毕设选题推荐:基于Java+SpringBoot物品租赁管理系统【源码+文档+调试】
java·vue.js·spring boot·mysql·spark·毕业设计·课程设计
hdsoft_huge1 天前
Java & Spring Boot常见异常全解析:原因、危害、处理与防范
java·开发语言·spring boot
AD钙奶-lalala1 天前
SpringBoot实现WebSocket服务端
spring boot·后端·websocket
毕设源码-朱学姐1 天前
【开题答辩全过程】以 4S店汽车维修保养管理系统为例,包含答辩的问题和答案
java·spring boot·汽车
BXCQ_xuan2 天前
软件工程实践二:Spring Boot 知识回顾
java·spring boot·后端