我有一个包含键和值的属性文件,如下所示:
TC_name1=true
Tc_name2=false
我需要一个Ant脚本来遍历这个属性文件,并只获得值等于true的键。
我还想知道是否可以使用。ini文件作为我们的属性文件。
请帮我解决这个问题,因为我在谷歌上搜索了一下,但没有找到解决方案。
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
输出: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