java根据不同环境使用不同的配置文件

一、maven编译打包方式 

<profiles>
    <profile>
        <id>preonline</id>
        <build>
            <resources>
                <resource>
                    <directory>src/main/resources/preonline/</directory>
                </resource>
            </resources>
        </build>
    </profile>

    <profile>
        <id>online</id>
        <activation>
            <activeByDefault>false</activeByDefault>
        </activation>
        <build>
            <resources>
                <resource>
                    <directory>src/main/resources/online/</directory>
                </resource>
            </resources>
        </build>
    </profile>

    <profile>
        <id>test</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <build>
            <resources>
                <resource>
                    <directory>src/main/resources/test/</directory>
                </resource>
            </resources>
        </build>
    </profile>
</profiles>
    static {
        Properties properties = new Properties();
        InputStream inputStream = Object.class.getResourceAsStream("/config.properties"); //这里必须是以/开始路径
        try {
            properties.load(inputStream);
            cluster = properties.getProperty("zkClusters.address");
            timeout = Integer.valueOf(properties.getProperty("zkClusters.timeout"));
            env = properties.getProperty("zkClusters.env");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
通过以下三种mvn命令进行分别打包
mvn clean package -Ptest
mvn clean package -Ppreonline
mvn clean package -Ponline

二、指定环境变量加载不同的配置文件

首先将配置文件统一放到一个目录中,然后我们在jvm虚拟机的时候通过自定义参数-D用于区分使用的配置文件,例如:-Dprofiles=preonline。

其实springboot也是这样实现的,通过 -Dspring.profiles.active=

三、其他

<build><finalname>XXXX</finalname></build>   -->  设置最终生成文件名称

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值