Java 配置文件动态选择Maven <Profiles>

应用背景

在开发过程中,Java运行会面临不同的运行环境,比如开发环境、测试环境、生产环境,在不同的开发环境下所需要配置的数据信息可能不一样,比如数据源配置、日志文件配置、以及一些软件运行过程中的基本配置,那每次我们将软件部署到不同的环境时,都需要修改相应的配置文件,为了避免这些繁琐的操作以及出现运行错误,maven提供了一种方便的解决这种问题的方案,通过进行pom文件的配置。

pom文件配置 Profiles

在项目的profile中添加如下的profile配置:


    <profiles>
        <profile>
        	<!-- 本地环境-->
            <id>loc</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <environment>loc</environment>
            </properties>
        </profile>

        <profile>
        	<!-- 开发环境-->
            <id>pms</id>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
            <properties>
                <environment>pms</environment>
            </properties>
        </profile>

        <profile>
        	<!-- 生产环境-->
            <id>prod</id>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
            <properties>
                <environment>prod</environment>
            </properties>
        </profile>

    </profiles>

这里定义了三个环境,分别是loc(本地环境)、pms(开发环境)、prod(生产环境),其中本地环境是默认激活的(activeByDefault 为true)。

<activeByDefault>true</activeByDefault>

配置文件

针对不同的开发环境,我们定义了不同的配置文件,文件目录如下:
在这里插入图片描述
如图所示,本地环境、开发环境、生产环境的配置文件分别放到src/main/resources目录下。

maven资源插件配置

在pom.xml的build结点下,配置资源文件的位置,如下所示:

<build>
        <finalName>pms</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>

        <resources>
            <resource>
            	<!-- 定义了变量配置文件的地址 -->
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/**.xml</include>
                    <include>application.yml</include>
                    <include>application-${environment}.yml</include>
                </includes>
                <!-- 一定要设置成true -->
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>

这里注意一个参数 filtering ,一定要设置成true.这样才会用对应${environment}的配置文件覆盖原来的。

yml配置文件指定pom文件中的配置项

在 application.yml 公共配置文件中需指定profiles的激活项:

spring:
  profiles:
    active: @environment@

@environment@ 参数指向的是pom文件中的 environment 标签,需使用@符号。
在这里插入图片描述
然后程序在运行时根据拼接的文件名,读取对应配置文件下的配置数据源。
在这里插入图片描述

实例如下

配置完成后,idea则有如下快捷选项,若指定Profiles下的 loc
在这里插入图片描述
application-loc.yml 内的配置内容为:

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    #    本地
    url: jdbc:mysql://localhost:3306/mallpms?useSSL=false&useUnicode=true&characterEncoding=UTF8
    username: root
    password: 654321
    type: com.alibaba.druid.pool.DruidDataSource

若指定Profiles下的 prod,application-prod.yml内的配置内容为:

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    #正式
    url: jdbc:mysql://192.168.1.102/mallpms?useSSL=false
    username: root
    password: 123456
    type: com.alibaba.druid.pool.DruidDataSource

启动项目后,Java会自动读取相应配置文件中的数据源,实现不同开发环境下配置文件的动态选择。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值