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

相关推荐
计算机-秋大田2 小时前
基于微信小程序的电子竞技信息交流平台设计与实现(LW+源码+讲解)
spring boot·后端·微信小程序·小程序·课程设计
customer085 小时前
【开源免费】基于SpringBoot+Vue.JS景区民宿预约系统(JAVA毕业设计)
java·vue.js·spring boot·后端·开源
MyY_DO5 小时前
maven mysql jdk nvm node npm 环境安装
java·mysql·maven
精通HelloWorld!9 小时前
使用HttpClient和HttpRequest发送HTTP请求
java·spring boot·网络协议·spring·http
拾忆,想起11 小时前
如何选择Spring AOP的动态代理?JDK与CGLIB的适用场景
spring boot·后端·spring·spring cloud·微服务
customer0813 小时前
【开源免费】基于SpringBoot+Vue.JS美食推荐商城(JAVA毕业设计)
java·vue.js·spring boot·后端·开源
一 乐13 小时前
基于微信小程序的酒店管理系统设计与实现(源码+数据库+文档)
java·数据库·vue.js·spring boot·微信小程序·酒店管理系统
小万编程19 小时前
【2025最新计算机毕业设计】基于SpringBoot+Vue家政呵护到家护理服务平台(高质量源码,可定制,提供文档,免费部署到本地)
java·vue.js·spring boot·计算机毕业设计·java毕业设计·web毕业设计
XYu123011 天前
Spring Boot 热部署实现指南
java·ide·spring boot·intellij-idea
是小崔啊1 天前
Spring Boot - 数据库集成07 - 数据库连接池
数据库·spring boot·oracle