资源属性过滤
需求
Maven 为了能够通过 profile 实现各不同运行环境切换,提供了一种『资源属性过滤』的机制。通过属性替换实现不同环境使用不同的参数。
配置
<profiles>
<profile>
<id>devJDBCProfile</id>
<properties>
<dev.jdbc.user>root</dev.jdbc.user>
<dev.jdbc.password>atguigu</dev.jdbc.password>
<dev.jdbc.url>http://localhost:3306/db_good</dev.jdbc.url>
<dev.jdbc.driver>com.mysql.jdbc.Driver</dev.jdbc.driver>
</properties>
<build>
<resources>
<resource>
<!-- 表示为这里指定的目录开启资源过滤功能 -->
<directory>src/main/resources</directory>
<!-- 将资源过滤功能打开 -->
<filtering>true</filtering>
</resource>
</resources>
</build>
</profile>
</profiles>
创建待处理的资源文件
dev.user=${dev.jdbc.user}
dev.password=${dev.jdbc.password}
dev.url=${dev.jdbc.url}
dev.driver=${dev.jdbc.driver}
执行处理资源命令
mvn clean resources:resources -PdevJDBCProfile
找到处理得到的资源文件
其中resource 标签
- includes:指定执行 resource 阶段时要包含到目标位置的资源
- excludes:指定执行 resource 阶段时要排除的资源
<build>
<resources>
<resource>
<!-- 表示为这里指定的目录开启资源过滤功能 -->
<directory>src/main/resources</directory>
<!-- 将资源过滤功能打开 -->
<filtering>true</filtering>
<includes>
<include>*.properties</include>
</includes>
<excludes>
<exclude>happy.properties</exclude>
</excludes>
</resource>
</resources>
</build>
执行处理资源命令:
mvn clean resources:resources -PdevJDBCProfile