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

相关推荐
皮皮林55123 分钟前
拒绝写重复代码,试试这套开源的 SpringBoot 组件,效率翻倍~
java·spring boot
用户908324602733 天前
Spring AI 1.1.2 + Neo4j:用知识图谱增强 RAG 检索(上篇:图谱构建)
java·spring boot
用户8307196840823 天前
Spring Boot 集成 RabbitMQ :8 个最佳实践,杜绝消息丢失与队列阻塞
spring boot·后端·rabbitmq
Java水解3 天前
Spring Boot 视图层与模板引擎
spring boot·后端
Java水解3 天前
一文搞懂 Spring Boot 默认数据库连接池 HikariCP
spring boot·后端
洋洋技术笔记4 天前
Spring Boot Web MVC配置详解
spring boot·后端
初次攀爬者4 天前
Kafka 基础介绍
spring boot·kafka·消息队列
用户8307196840824 天前
spring ai alibaba + nacos +mcp 实现mcp服务负载均衡调用实战
spring boot·spring·mcp
Java水解4 天前
SpringBoot3全栈开发实战:从入门到精通的完整指南
spring boot·后端
初次攀爬者5 天前
RocketMQ在Spring Boot上的基础使用
java·spring boot·rocketmq