spring多个环境配置(dev、test、uat、prod)

常用的环境有开发环境dev、测试环境test、验收环境uat、生产环境prod。多环境配置可以有效的进行数据库及其他数据的配置,是常用的手法。

多环境配置常用的有两种方式

方式一、复制多个application.properties文件,并根据环境重命名。

复制多个不同环境的配置文件 命名例如:application-dev.properties、application-test.properties、application-uat.properties、application-prod.properties

然后在application.properties配置文件
添加如下内容,

##指定环境
spring.profiles.active=prod

方式二、将不同环境的配置文件放在不同的文件目录下。

先在resource文件中新建目录结构,开发环境是dev目录,测试环境是test目录,验收环境是uat目录,生产环境是prod目录。每个目录下面都有application.properties文件。

在不同环境的配置文件添加如下配置:
示例:

##开发环境配置文件
multiple.environment.configure=dev environment
##测试环境配置文件
multiple.environment.configure=test environment

然后启动类module中的pom.xml文件中配置如下:

<profiles>
        <!-- 开发环境 -->
        <profile>
            <id>dev</id>
            <properties>
                <profiles.active>dev</profiles.active><!-- 配置文件目录的名字 -->
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault> <!-- 默认启动dev环境 -->
            </activation>
        </profile>
        <!-- 测试环境 -->
        <profile>
            <id>test</id>
            <properties>
                <profiles.active>test</profiles.active>
            </properties>
        </profile>
         <!-- uat环境 -->
        <profile>
            <id>uat</id>
            <properties>
                <profiles.active>uat</profiles.active>
            </properties>
        </profile>
        <!-- 生产环境 -->
        <profile>
            <id>prod</id>
            <properties>
                <profiles.active>prod</profiles.active>
            </properties>
        </profile>
    </profiles>

<build>
        <finalName>app</finalName>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <!-- 打包时删除以下目录 -->
                <excludes>
                    <exclude>dev/*.properties</exclude>
                    <exclude>test/*.properties</exclude>
                    <exclude>uat/*.properties</exclude>
                    <exclude>prod/*.properties</exclude>
                </excludes>
            </resource>
            <resource>
                <!-- 这个就是切换不同环境的路径,${profiles.active}profiles.active,可自定义 -->
                <directory>src/main/resources/${profiles.active}</directory>
                <filtering>true</filtering>
            </resource>
        </resources>

        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <skip>
                        true
                    </skip>
                </configuration>
            </plugin>
        </plugins>
    </build>

以上打包我是通过IDEA的maven窗口来打包的

也可以使用maven命令打包:

开发环境打包

mvn clean package -P dev

测试环境打包

mvn clean package -P test

生产环境打包

mvn clean package -P prod

  • 10
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值