Maven 使用profile来区分开发、测试、生产环境

Maven允许用户提供自定义的Profile(配置集)来应对在不同场景下配置不同的问题。这样做的好处是将配置做隔离,在打包时,只需要选中的Profile package就行。实现方式是结合Maven的过滤器来处理占位符的替换

来看XML配置

<!--定义配置文件-->
    <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <env>dev</env>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <env>test</env>
            </properties>
        </profile>
        <profile>
            <id>release</id>
            <properties>
                <env>release</env>
            </properties>
        </profile>
        <profile>
            <id>product</id>
            <properties>
                <env>product</env>
            </properties>
        </profile>
    </profiles>
    <build>
        <filters>
            <!--这里定义配置资源的目录,并且根据变量不同来选取不同的目录-->
            <filter>src/main/config/${env}.properties</filter>
        </filters>
        <resources>
            <resource>
                <!--这里定义需要处理的配置资源路径-->
                <directory>src/main/resources</directory>
                <!--启用过滤-->
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
这样定义完以后,就可以在Maven栏中看到这些配置

默认dev是勾选的,因为配置了activeByDefault为true

新建prop.properties文件

environment=${environment}

在目录src/java/main/config新建四个配置文件

dev.properties

environment=dev

test.properties

environment=test

release.properties

environment=release
product.properties

environment=product

bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd 
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <context:property-placeholder location="prop.properties"/>
    <bean id="profileTest" class="org.zheng.profile.ProfileTest">
        <property name="environment" value="${environment}"/>
    </bean>
</beans>

测试代码

public class ProfileTest {
    private String environment;

    public String getEnvironment() {
        return environment;
    }

    public void setEnvironment(String environment) {
        this.environment = environment;
    }

    public static void main(String[] args) throws IOException {
        ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
        ProfileTest profileTest = (ProfileTest)ac.getBean("profileTest");
        System.out.println("env="+profileTest.getEnvironment());
    }
}

执行查看输出结果:


由此看到,变量被替换为dev.properties中的值。构建maven,也将自动替换格式为"${xxx}"为当前激活的配置集中的内容。

完整项目地址:

https://github.com/difffate/JavaProject


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值