Maven 使用 profile 和 filtering 实现多种环境下的资源配置管理
Filtering 功能Filtering 是 Maven Resources Plugin 的一个功能,它会使用系统属性或者项目属性的值替换资源文件(*.properties,*.xml)当中 ${…} 符号的值。比如你系统属性有一项 “user.name=foobar”,那么资源文件当中的 ${user.name} 符号会在 Maven 编译时自动被替换为 “foobar”。
Profile 功能Profile 的作用是允许你在项目文件(pom.xml)里定义若干个 profile 段,然后在编译时选择其中的一个用于覆盖项目文件原先的定义。
<profiles> <profile> <id>online</id> <activation> <activeByDefault>false</activeByDefault> </activation> <properties> <build.profile.id>online</build.profile.id> </properties> <build> <filters> <filter>profiles/online/config.properties</filter> </filters> <resources> <resource> <filtering>true</filtering> <directory>src/main/resources</directory> <includes> <include>jdbc.properties</include> <include>log4j.properties</include> <include>system.properties</include> <include>alarm.properties</include> </includes> </resource> <resource> <filtering>false</filtering> <directory>src/main/resources</directory> </resource> </resources> </build> </profile> <profile> <id>local</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <build.profile.id>local</build.profile.id> </properties> <build> <filters> <filter>profiles/${build.profile.id}/config.properties</filter> </filters> <resources> <resource> <filtering>true</filtering> <directory>src/main/resources</directory> <includes> <include>jdbc.properties</include> <include>log4j.properties</include> <include>system.properties</include> <include>alarm.properties</include> </includes> </resource> <resource> <filtering>false</filtering> <directory>src/main/resources</directory> </resource> </resources> </build> </profile> </profiles>