Ant 遍历属性文件

Ant 遍历属性文件,ant,Ant,我有一个包含键和值的属性文件,如下所示: TC_name1=true Tc_name2=false 我需要一个Ant脚本来迭代这个属性文件,并只获取值等于true的键 我还想知道是否可以使用.ini文件作为我们的属性文件 请帮我解决这个问题,因为我在谷歌上搜索,但找不到解决方案。build.xml: <?xml version="1.0" encoding="UTF-8"?> <project default="load"> <target name="l

我有一个包含键和值的属性文件,如下所示:

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>
输出:

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
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>
输出:

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

+1适用于无外部插件的解决方案。就我个人而言,我会嵌入一个groovy脚本,但这需要一个外部jar.+1,才能在没有外部插件的情况下工作。就我个人而言,我会嵌入一个groovy脚本,但这需要一个外部jar