I assume that reader has a basic understanding of Maven resources.
Simple resource filtering
Let's generate the project:$ mvn archetype:generate -DgroupId=org.halyph -DartifactId=proptest -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
Now, we have an application property file with bunch of properties which must be filtered \proptest\src\main\resources\application.properties:4.0.0 org.halyph proptest jar 1.0-SNAPSHOT proptest http://maven.apache.org junit junit 3.8.1 test
application.username=${jdbc.username} application.password=${jdbc.password} application.databaseName=${jdbc.databaseName}We have to add build/resources and properties section into pom.xml:
Lets run maven resource filtering and check the resulted \proptest\target/classes/application.properties file:4.0.0 org.halyph proptest jar 1.0-SNAPSHOT proptest http://maven.apache.org junit junit 3.8.1 test src/main/resources true default_username default_password default_databaseName
$ mvn process-resources $ cat target/classes/application.properties application.username=default_username application.password=default_password application.databaseName=default_databaseNameAs you can see property values were successfully substituted.
Resource filtering with external properties file
We can extract Maven properties in external property file \proptest_extfile\src\main\filters\mysql_filters.properties:jdbc.username=mysql_username jdbc.password=mysql_password jdbc.databaseName=mysql_databaseNamealso, we have to adjust pom.xml to work properly with external filters (added build/filters and removed properties):
Let's check the resulted \proptest\target\classes\application.properties file:4.0.0 org.halyph proptest jar 1.0-SNAPSHOT proptest http://maven.apache.org junit junit 3.8.1 test src/main/filters/mysql_filters.properties src/main/resources true
$ mvn process-resources $ cat target/classes/application.properties application.username=mysql_username application.password=mysql_password application.databaseName=mysql_databaseName
Mixed resource filtering with external/internal properties
What happen if we have overlapped properties in pom.xml with external property file.Modified pom.xml:
Now, check the resulted \proptest\target\classes\application.properties file:4.0.0 org.halyph proptest jar 1.0-SNAPSHOT proptest http://maven.apache.org junit junit 3.8.1 test src/main/filters/mysql_filters.properties src/main/resources true default_databaseName
$ mvn process-resources $ cat target/classes/application.properties application.username=mysql_username application.password=mysql_password application.databaseName=default_databaseNameIn this case Maven uses application.databaseName property from pom.xml
Managing properties with properties-maven-plugin
Please check the plugin home page for more details http://mojo.codehaus.org/properties-maven-pluginThis plugin read external property and they behave like were declared in pom.xml.
Modified pom.xml:
Also, we have to add new external property file just to verify the property overlapping issue:4.0.0 org.halyph proptest jar 1.0-SNAPSHOT proptest http://maven.apache.org junit junit 3.8.1 test org.codehaus.mojo properties-maven-plugin 1.0-alpha-2 initialize read-project-properties src/main/filters/cust_mysql_filters.properties src/main/filters/mysql_filters.properties src/main/resources true
\proptest_extfile\src\main\filters\cust_mysql_filters.properties:
jdbc.username=cust_mysql_username jdbc.password=cust_mysql_password jdbc.databaseName=cust_mysql_databaseNameNow, it's time to check the resulted file \proptest\target\classes\application.properties file:
$ mvn process-resources $ cat target/classes/application.properties application.username=cust_mysql_username application.password=cust_mysql_password application.databaseName=cust_mysql_databaseName
Have you noticed that we've got properties values from cust_mysql_filters.properties and Maven hasn't applied properties from mysql_filters.properties file?
So, as you can see we have several ways for managing/filtering properties with Maven. And, it's very convenient.
You can checkout code snippets from github