自学内容网 自学内容网

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来做,首先配置如下:

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


原文地址:https://blog.csdn.net/wang0907/article/details/142786102

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!