使用Ant从属性文件中检索所有键

时间:2014-02-21 13:04:16

标签: ant properties

我有一个包含键/值对的属性文件:

key1=value1
key2=value2
...

如何使用Ant检索此文件中所有键的列表?

1 个答案:

答案 0 :(得分:3)

loadfile使用filterchain,f.e。 :

<project>

  <!-- given some file with :
       key=value
       key=someothervalue
        ...
   -->

   <loadfile property="keysonly" srcfile="some.properties">
     <filterchain>
       <tokenfilter>
           <replaceregex pattern="(.+)=.+" replace="\1"/>
        </tokenfilter>
      </filterchain>
    </loadfile>

    <echo>${keysonly}</echo>

</project>

如果你需要特殊形式的钥匙,f.e。逗号分隔使用类似:

<loadfile property="keysonly" srcfile="some.properties">
  <filterchain>
    <tokenfilter>
      <!-- use some delimiter f.e. '###' -->
      <replaceregex pattern="(.+)=.+" replace="\1###"/>
    </tokenfilter>
      <!-- get rid of linefeeds -->
     <striplinebreaks/>
    <tokenfilter>
      <!-- replace delimiter '###' with ',' -->
      <replaceregex pattern="###" replace="," flags="g"/>
    </tokenfilter>
    <tokenfilter>
      <!-- replace dangling ',' -->
      <replaceregex pattern=",$" replace=""/>
    </tokenfilter>
  </filterchain>
</loadfile>