前言:
最近在项目中使用springboot时发现,采用在pom中定义不同的profile,并且maven打包时
采用-P参数并不能替换我application.properties文件中指定占位符的问题。
配置文件布局:

在application.properties中定义整个项目中不同环境下共通的配置属性,并采用springboot针对配置文件的特性 - spring.profiles.active=dev或者test
来自动加载合并application-dev/test.properties中特有的配置的功能,开发,测试,生产环境配置文件命名方面需要遵守springboot的约束即可。
Maven pom 配置:


备注:实际上使用springboot,上述的<resources>标签,以及<outputDirectory>都是不需要的,其都已经定义过了。在这里写出来只是为了讲述maven打包的原理。
执行打包命令:
进入项目pom文件所在目录,执行
mvn clean package -P test
命令解释:
1:执行后会根据pom中定义的profiles标签寻找对应的profile id为test的,将其properties下的子标签获取到,拿到key = boot.profile,value = test。放入map中
:2:根据 build中定义的resources标签,开始打包对应的资源文件,由于指定了filtering为true,故会将上一步中得到的map中的key拿去替换resource中指定的资源文件中的占位符${key},为value值。
遇到的问题:

执行后查看classes文件下的资源文件,发现并没有替换掉
原因:
发现最终是因为springboot导致的,查看其pom继承,

进入后发现,springboot默认指定资源文件中的占位符为@@,并不是maven默认的 ${}


解决方案:
1:将application.properties中的占位符由${key} -> @key@
2:覆盖springboot的默认规则

3:测试了再自己的pom中不增加resource配置,只是增加

也是可以的哦
建议:
既然已经使用了springboot,就不要再用回之前的写法了,平白增加配置。
网友评论