Reputation: 91
I have a property file containing keys and values as shown below:
TC_name1=true
Tc_name2=false
I need an Ant script to iterate through this property file and get only the keys with value equal to true.
Also I want to know whether it is possible to use an .ini file as our property file.
Please help me out with this as I googled, but was not able to find the solution.
Upvotes: 1
Views: 2032
Reputation: 11110
build.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project default="load">
<target name="load">
<!-- load the properties as plain text into a property -->
<loadfile property="trueProps" srcfile="load.properties">
<!-- only load affirmative values of properties -->
<filterchain>
<linecontainsregexp>
<regexp pattern="=\s*(true|yes|on)\s*$" />
</linecontainsregexp>
</filterchain>
<!-- prefix for easy retrieval -->
<filterchain>
<prefixlines prefix="testing." />
</filterchain>
</loadfile>
<!-- print the property contents -->
<echo taskname="trueProps">${trueProps}</echo>
<!-- extract the keys -->
<loadresource property="trueKeys" >
<string>${trueProps}</string>
<filterchain>
<replaceregex pattern="=\s*(true|yes|on)\s*$" replace="" />
</filterchain>
</loadresource>
<!-- print the keys -->
<echo taskname="trueKeys">${trueKeys}</echo>
<!-- load the contents of the variable as ant properties -->
<loadproperties>
<string>${trueProps}</string>
</loadproperties>
<!-- confirm the properties are loaded as expected -->
<echoproperties prefix="testing." />
</target>
</project>
load.properties:
prop1=false
some_other=true
also_true=yes
yet_another=on
not_this=no
TC_name1=true
Tc_name2=false
some_other_match =yes
not_a_yes= false
output:
Buildfile: build.xml
load:
[trueProps] testing.some_other=true
[trueProps] testing.also_true=yes
[trueProps] testing.yet_another=on
[trueProps] testing.TC_name1=true
[trueProps] testing.some_other_match =yes
[trueKeys] testing.some_other
[trueKeys] testing.also_true
[trueKeys] testing.yet_another
[trueKeys] testing.TC_name1
[trueKeys] testing.some_other_match
[echoproperties] #Ant properties
[echoproperties] #Wed May 29 10:57:26 EDT 2013
[echoproperties] testing.TC_name1=true
[echoproperties] testing.also_true=yes
[echoproperties] testing.some_other=true
[echoproperties] testing.some_other_match=yes
[echoproperties] testing.yet_another=on
BUILD SUCCESSFUL
Total time: 0 seconds
Upvotes: 4