Monday, June 11, 2012

Maven Resource Filtering

This post is inspired by:
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


  4.0.0
  org.halyph
  proptest
  jar
  1.0-SNAPSHOT
  proptest
  http://maven.apache.org
  
    
      junit
      junit
      3.8.1
      test
    
   

Now, we have an application property file with bunch of properties which must be filtered \proptest\src\main\resources\application.properties:
application.username=${jdbc.username}
application.password=${jdbc.password}
application.databaseName=${jdbc.databaseName}
We have to add build/resources and properties section into pom.xml:

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
  



Lets run maven resource filtering and check the resulted \proptest\target/classes/application.properties file:

$ mvn process-resources
$ cat target/classes/application.properties

application.username=default_username
application.password=default_password
application.databaseName=default_databaseName
As 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_databaseName
also, we have to adjust pom.xml to work properly with external filters (added build/filters and removed properties):

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
      
    



Let's check the resulted \proptest\target\classes\application.properties file:
$ 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:

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



Now, check the resulted \proptest\target\classes\application.properties file:
$ mvn process-resources
$ cat target/classes/application.properties

application.username=mysql_username
application.password=mysql_password
application.databaseName=default_databaseName
In 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-plugin

This plugin read external property and they behave like were declared in pom.xml.
Modified pom.xml:

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
      
    



Also, we have to add new external property file just to verify the property overlapping issue:
\proptest_extfile\src\main\filters\cust_mysql_filters.properties:
jdbc.username=cust_mysql_username
jdbc.password=cust_mysql_password
jdbc.databaseName=cust_mysql_databaseName
Now, 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

1 comment:

  1. If you need a l10n tool to automate the translation of .properties files, that is also integrated with GitHub, try https://poeditor.com/
    It's useful to automate the localization workflow and for team work.

    ReplyDelete