maven resource profile ${} 无法在 spring boot 使用的原因

在springmvc开发的时候,我们常常利用maven命令:package -P dev 来打包不同环境的配置文件,例如logback.xml,开发时日志级别为debug,生产为info

<!-- logback.xml -->
<root level="${maven.log.levle}">
    
</root> 
	<!--pom.xml  -->
	<resources>
		<resource>
			<directory>src/main/resources</directory>
			<filtering>true</filtering>
		</resource>
	</resources>

	<profiles>
		<profile>
			<id>dev</id>
			<activation>
				<activeByDefault>true</activeByDefault>
			</activation>
			<properties>
				<maven.log.level>debug</maven.log.level>
			</properties>
		</profile>
		<profile>
			<id>prd</id>
			<properties>
				<maven.log.level>info</maven.log.level>
			</properties>
		</profile>
	</profiles>

但是在spring boot 打包时,你会发现${maven.log.levle}这个变量没有被替换,why?在spring-boot-starter-parent的pom中,maven-resources-plugin被重新定义了,为了避免与${}冲突,只能使用@

<artifactId>spring-boot-starter-parent</artifactId>

<properties>
	<resource.delimiter>@</resource.delimiter>
</properties>

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-resources-plugin</artifactId>
	<configuration>
		<delimiters>
			<delimiter>${resource.delimiter}</delimiter>
		</delimiters>
		<useDefaultDelimiters>false</useDefaultDelimiters>
	</configuration>
</plugin>

因此只需要这样修改即可,如果变量中本来就需要输出@,可以用\@进行转义

<!-- logback.xml -->
<root level="@maven.log.levle@">
    
</root> 

spring boot关于maven的说明:

https://docs.spring.io/spring-boot/docs/1.5.x/reference/htmlsingle/#howto-automatic-expansion-maven

maven-resources-plugin插件说明:

http://maven.apache.org/plugins/maven-resources-plugin/resources-mojo.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值