Maven的profile使用

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中内容是生成环境的,表示成功。

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值