profile filters properties 无法替换问题总结

本文介绍如何利用Maven pom配置中的profiles功能,根据不同环境使用不同的配置文件,包括创建资源文件、导入配置、配置环境激活及打包命令的执行。

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

基于maven pom配置中的profiles配置针对不同环境,使用不同配置文件。

 

第一步:创建filters资源文件(src/main/filters/dev.properties、test.properties、beta.properties),其中路径地址/名称任意设置。

dev.properties

 

mysql.url=jdbc:mysql://xx.xx.xx.xx:3306/xx

 

 

第二步:创建配置文件(src/main/resources/settings.properties),对第一步配置资源进行使用。

settings.properties

 

mysql.url=${mysql.url}

 可使用如下命令将配置文件导入到spring容器中

<context:property-placeholder location="classpath:settings.properties"/>

 

也可在spring配置文件中直接使用(src/main/resources/spring.xml)

spring.xml

 

<bean  class="xx.xx.xx.xx.xx.xx">
    <property name="url" value="${mysql.url}" />
</bean>

 

第三步:在pom中配置profile

    pom.xml 

<profiles>
	<profile>
		<id>dev</id>
		 <build>
			<filters>
				<filter>src/main/filters/dev.properties</filter>
			</filters>
		 </build>
                 <!-- 定义其为默认加载 -->
		 <activation>
			<activeByDefault>true</activeByDefault>
		 </activation>
	</profile>
	<profile>
		<id>test</id>
		 <build>
			<filters>
				<filter>src/main/filters/test.properties</filter>
			</filters>
		 </build>
	</profile>
	<profile>
		<id>beta</id>
		 <build>
			<filters>
				<filter>src/main/filters/beta.properties</filter>
			</filters>
		 </build>
	</profile>

 将其添加到pom的<project>标签内部。

 

第四步(关键一步):maven打包时激活filters过滤。

pom.xml

<build>
         <!-- 定义项目打包后的名称 -->
	<finalName>pojo-name</finalName>
	<resources>
		<resource>
                         <!-- 对该目录下的配置build时启用filter 过滤替换-->
			<directory>src/main/resources</directory>
			<filtering>true</filtering>
		</resource>
	</resources>
</build>

 

 

第五步:执行打包命令

mvn package -P dev

其中 -P 指定编译的profile

 

问题分享:

在使用过程中,配置与以上配置完全相同,但始终无法对${mysql.url}等参数进行替换,最终导致的问题是,我在eclipse-->properties-->java build path-->source 配置中将src/main/resources默认编译到了src/main/webapp/WEB-INF/classes 目录下(未替换文件-错误文件),导致在执行mvn 命令时,由于未进行mvn清理,mvn是直接将src/main/webapp/WEB-INF/classes拷贝到了target/obj-name/WEB-INF/下面,而不是经过build filter重新生成的配置文件!解决方案:删除src/main/webapp/WEB-INF/classes下配置文件,让其在mvn package -P dev 时重新生成!

 

 

 

 

 

### Java 根据 Profile 配置选择不同服务前缀 在Java应用程序中,特别是基于Spring Boot的应用程序,可以通过多种方式根据激活的Profile来动态选择不同的服务前缀。以下是几种常见的实现方法: #### 方法一:使用 Spring Boot 的 `@Value` 注解结合多环境配置文件 Spring Boot 支持通过命名约定自动加载特定于环境的属性文件。例如,在资源目录下创建名为 `application-dev.properties` 和 `application-prod.properties` 文件用于开发和生产环境。 ```properties # application-dev.properties service.url=http://dev.example.com/api/ ``` ```properties # application-prod.properties service.url=https://prod.example.com/api/ ``` 然后在代码中注入这些值: ```java import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class ServiceConfig { @Value("${service.url}") private String serviceUrl; public String getServiceUrl() { return this.serviceUrl; } } ``` 这种方法简单易用,并且完全依赖框架本身的能力[^2]。 #### 方法二:自定义 PropertyPlaceholderConfigurer 或 EnvironmentPostProcessor 对于更复杂的场景,可能需要编写自己的逻辑来处理属性替换。这可以通过扩展 `PropertySourcesPlaceholderConfigurer` 来完成,或者实现 `EnvironmentPostProcessor` 接口来自定义环境初始化过程中的行为。 这种方式允许更加灵活地控制如何以及何时应用某些设置,但同时也增加了复杂度。 #### 方法三:利用 Maven Profiles 控制编译期常量 如果希望在构建阶段就确定最终的服务地址,则可以考虑借助Maven Profles特性。通过修改POM文件内的 `<profiles>` 节点并配合过滤器功能,可以在打包过程中将占位符替换成实际值。 ```xml <build> <filters> <filter>src/main/resources/${env}.properties</filter> </filters> ... </build> <profiles> <profile> <id>development</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <env>dev</env> </properties> </profile> <!-- Other profiles --> </profiles> ``` 上述例子展示了如何依据当前激活的Profile从相应的`.properties`文件读取数据[^1]。 以上三种方案各有优劣,具体采用哪一种取决于项目的实际情况和技术栈的选择。无论哪种情况,都建议保持良好的文档记录习惯以便后续维护人员理解意图。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值