2014-07-09 21 views
0

我有一个属性,它的值为用逗号分隔的表示数字的字符串列表。例如,存储在属性中的逗号分隔列表中的前缀项

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

为了讨论起见,我有一个名为“reals”的目录。 其中有'number.one','number.two'和'number.three'等名称的子目录以及其他我希望忽略的子目录。

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

喜欢的东西

<dirset id="something" includes="${test.property}" dir="reals"/> 

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

有没有什么办法解决这个问题,仅仅使用我描述过的属性,而不是提供一个已经有正确格式的test.property列表的任务?

回答

1

可以使用ant-contrib任务PropertyRegex任务,像这样的东西

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

谢谢,这很好。 – daqpan

+0

不客气。 – aliteralmind

+0

btw。在你的个人资料中年龄= 94似乎是一个错字! – Rebse

1

为了让您的dirset包括对应的子目录编辑+覆盖与脚本任务现有test.property和内置的JavaScript引擎:

<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的属性。

+0

太棒了。我一直在使用Ant多年,从不知道脚本语言可用。 – aliteralmind

+0

当使用内置javascript引擎的脚本任务时,您可以完全访问ant api,并且不需要任何额外的jar文件,如f.e. antcontrib。 Groovy也是推荐的 - 你需要一个jar包 - groovy的任务非常棒。 – Rebse

相关问题