存储在属性中的逗号分隔列表中的前缀项

时间:2014-07-09 23:22:01

标签: ant

我有一个属性,其值为逗号分隔的表示数字的字符串列表。例如,

test.property =一个,两个,三个 它可以是任何数字列表,但表格将是相同的。

为了论证,我有一个名为“reals'”的目录。 其中包含名称为' number.one',' number.two'的子目录。和' number.three'等等以及其他一些我想忽略的子目录。

我想获得一些与test.property中的条目对应的子目录列表

这样的东西
<dirset id="something" includes="${test.property}" dir="reals"/>

这里的问题是test.property定义的列表中的项目都需要以&#39;数字为前缀。&#39;为此工作。我不知道该怎么做,这构成了我问题的第一部分。

有没有办法解决这个问题,只使用我所描述的属性,而不是以正确的格式为任务提供test.property列表?

2 个答案:

答案 0 :(得分:1)

您可以使用ant-contrib任务PropertyRegex任务,类似

<propertyregex property="${comma.delimed.nums}"
   input="package.ABC.name"
   regexp="\b(\w+)\b"
   replace="number.\1"
   global="true"
   casesensitive="false" />

答案 1 :(得分:1)

使您的dirset包含相应的子目录编辑+使用脚本任务和内置的javascript引擎覆盖现有的test.property:

<project>

<property name="test.property" value="one,two,three"/>
<echo>1. $${test.property} => ${test.property}</echo>

<script language="javascript">
 <![CDATA[
  var items = project.getProperty('test.property').split(',');
  var s = "";

  for (i = 0; i < items.length; i++) {
   s += '*' + items[i] + ',';
  }

  project.setProperty('test.property', s.substring(0, s.length - 1));
 ]]>
 </script>   

<echo>2. $${test.property} => ${test.property}</echo>

<dirset id="something" includes="${test.property}" dir="C:\some\path"/>
<echo>Dirset includes => ${toString:something}</echo>

</project>

输出:

[echo] 1. ${test.property} => one,two,three
[echo] 2. ${test.property} => *one,*two,*three
[echo] Dirset => number.one;number.three;number.two

如果要创建新属性而不是覆盖现有的test.property,请使用:

project.setProperty('whatever', s.substring(0, s.length - 1));


project.setNewProperty('whatever', s.substring(0, s.length - 1));

并在dirset的include属性中使用新创建的属性。

相关问题