maven执行package命令解析配置文件中的占位符进行替换

本文介绍如何使用Maven在打包时自动替换配置文件中的占位符。通过配置<filters>和<resources>标签,实现从指定文件读取属性值并应用于目标配置文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

maven执行package命令解析配置文件中的占位符进行替换

需要在maven的<build>标签中配置两个地方 <filters><resources>

1、filters中配置要读取的配置的源文件
2、includes中配置需要解析占位符的配置文件

配置方式如下面的代码

<build>
     <finalName>spring-xml-read-property-config</finalName>
	<filters>
	<!-- filters中配置要读取的配置的源文件 -->
	<filter>src/main/resources/config/configfile.properties</filter>
	</filters>
    <resources>
         <resource>
             <directory>src/main/resources</directory>
             <includes>
             	<!-- includes中配置需要解析占位符的配置文件-->
                 <include>spring-mvc.xml</include>
             </includes>
             <filtering>true</filtering>
         </resource>
     </resources>
 </build>

我的spring-mvc.xml文件的配置,config.idconfig.name的值为动态值,需要从configfile.properties文件中进行读取

spring-mvc.xml

<bean id="configBean" class="cn.shutdown.web.bean.ConfigBean">
    <property name="id" value="${config.id}"/>
    <property name="name" value="${config.name}"/>
</bean>

configfile.properties

config.id=1
config.name=domino

配置了pom.xml的build配置以后,执行mvn clean package 命令,在target目录中可以看到spring-mvc.xml中的配置已经变成了 configfile.properties文件中的值

demo地址

https://gitee.com/j__domino/springboot-demo/tree/master/spring-config-demo/spring-xml-read-property-config

参考文档

https://www.iteye.com/blog/yaozhiqiang109-1901967

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值