1

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

TC_name1=true
Tc_name2=false

我需要一个 Ant 脚本来遍历这个属性文件并只获取值等于 true 的键。

另外我想知道是否可以使用 .ini 文件作为我们的属性文件。

请帮我解决这个问题,因为我用谷歌搜索,但找不到解决方案。

4

1 回答 1

4

构建.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>

加载属性:

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
于 2013-05-29T15:05:16.377 回答