[亲测]集成maven和Spring boot的profile-动态加载不同环境的配置文件

今天总结一下,项目中如何动态加载不同环境的配置文件

1.先看一下配置文件。配置文件挺多的,因为此项目比较大,开发环境(dev和dev1),测试环境(pre),生产环境(pro),云环境(ali)等
在这里插入图片描述
2.再来看一下主配置文件(application.yml)。注意圈中部分

  application:
    name: supplier-service
  profiles:
    active: '@profileActive@'

在这里插入图片描述
3.定义profile,看一下对应pom文件。拿这张图和图2比较可发现:图2中的active的值和图3中第二个圈中的值一样
定义完毕之后,当我们使用mvn clean package -P dev 时,maven就知道了profileActive=dev这个属性生效了。其中profileActive可以自己定义,就是一个maven的自定义属性

    <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <!-- 环境标识,需要与配置文件的名称相对应 -->
                <profileActive>dev</profileActive>
            </properties>
            <activation>
                <!-- 默认环境 -->
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <profileActive>test</profileActive>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <profileActive>prod</profileActive>
            </properties>
        </profile>
    </profiles>

在这里插入图片描述
4.定义filter,配置文件路径以及打包所需包含文件

<build>
        <finalName>supplier-service</finalName>
        <plugins>
            <plugin>
                <!-- The plugin rewrites your manifest -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>cn.com.casmart.supplier.SupplierApplication</mainClass>
                    <!--fork :  如果没有该项配置,肯呢个devtools不会起作用,即应用不会restart -->
                    <fork>true</fork>
                    <jvmArguments>-Dfile.encoding=UTF-8</jvmArguments>
                    <executable>true</executable>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>*.yml</include>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
    </build>

在这里插入图片描述
5.我们来测试一下刚刚的配置

public class SupplierApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext run = SpringApplication.run(SupplierApplication.class, args);
        String[] activeProfiles = run.getEnvironment().getActiveProfiles();
        for (String activeProfile : activeProfiles) {
            System.out.println("Spring Boot 使用profile为: "+activeProfile);
        }
    }
}

结果:
在这里插入图片描述
在这里插入图片描述

开发环境打包:mvn clean package -Dmaven.test.skip=true -P dev -e

这里的dev代表加载application.dev.yml配置文件

以下是我本地打包的配置文件效果
在这里插入图片描述

参考博客1
参考博客2

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值