profile简介:
在实际开发项目中,常常有几种环境,一般情况下有几种不同的环境。比如:开发,生产。各个环境之间的参数各不相同,比如MySQL、Redis等不同环境的参数不一样,若每个环境都手动替换环境很容易出错,这里我们利用maven的profile功能切换环境。
效果:
我们要的效果是通过maven打包时候参数,动态的生成自己想要的参数。
项目结构:
在我们的项目中有公共的配置,还有开发时用的配置,生产时用的配置。
创建messsage.properties。在dev下内容包为dev=dev,pro包下为pro=pro
在pom.xml配置profile:
<profiles>
<profile>
<!--构建配置的唯一标识符。即用于命令行激活,也用于在继承时合并具有相同标识符的profile。 -->
<id>dev</id>
<properties>
<!-- 环境标识,需要与配置文件的名称相对应 -->
<env>dev</env>
</properties>
<activation>
<!-- 默认激活该profile节点 -->
<activeByDefault>true</activeByDefault>
</activation>
<build>
<resources>
<resource>
<directory>src/main/resources-env/dev</directory>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
</profile>
<profile>
<!-- 生产环境 -->
<id>pro</id>
<properties>
<env>pro</env>
</properties>
<build>
<resources>
<resource>
<directory>src/main/resources-env/pro</directory>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
</profile>
</profiles>
到此我们就完成了maven的profile配置。在Eclipse中使用mvn打包:输入pro表示选择了id为pro的生产环境。
我们查看target目录下的jar包,解压:
发现在message.properties中内容是生成环境的,表示成功。
本博客为自己总结亦或在网上发现的技术博文的转载。 如果文中有什么错误,欢迎指出。以免更多的人被误导。
邮箱:[email protected]
版权声明:本文为博主原创文章,博客地址:https://blog.csdn.net/ChinaMuZhe,未经博主允许不得转载。
扫描二维码关注公众号,回复:
2095824 查看本文章