给定一个 CSV 蚂蚁属性,
<property name="module.list" value="mod1,mod2,mod3,mod4,mod5"/>
我怎样才能得到第一个元素(即这里的“mod1”)?我想执行一个将“mod1”作为参数之一的命令。
此外..我不能将这个原始的“module.list”属性修改为列表或其他任何东西..虽然我可以从中创建另一个列表、属性等..
任何帮助表示赞赏..谢谢
给定一个 CSV 蚂蚁属性,
<property name="module.list" value="mod1,mod2,mod3,mod4,mod5"/>
我怎样才能得到第一个元素(即这里的“mod1”)?我想执行一个将“mod1”作为参数之一的命令。
此外..我不能将这个原始的“module.list”属性修改为列表或其他任何东西..虽然我可以从中创建另一个列表、属性等..
任何帮助表示赞赏..谢谢
如果您想使用所有变量,还可以查看 Ant-Contrib 任务 for 和 foreach。
<echo message="The first five letters of the alphabet are:"/>
<for list="a,b,c,d,e" param="letter">
<sequential>
<echo>Letter @{letter}</echo>
</sequential>
</for>
http://ant-contrib.sourceforge.net/tasks/tasks/index.html
为了用户任务,不要忘记声明这个任务 def :
<taskdef resource="net/sf/antcontrib/antlib.xml" />
根据 module.list 的实际内容,您也许可以使用 pathconvert:
<project>
<property name="module.list" value="mod1,mod2,mod3,mod4,mod5"/>
<pathconvert property="module.1">
<path path="${module.list}"/>
<chainedmapper>
<flattenmapper/>
<mapper type="regexp" from="(.*?),.*" to="\1"/>
</chainedmapper>
</pathconvert>
<echo>${module.1}</echo>
</project>
该任务会进行大量的字符串操作,因此如果 module.list 的内容可以包含特殊的路径字符,那么这种方法将不起作用。在这种情况下,我会选择更通用的答案之一。
Ant-Contrib进行救援。
您可以使用propertyregex
Ant-Contrib 中的任务来提取逗号分隔字符串的第一部分,如下所示:
<propertyregex property="module.first"
input="${module.list}"
regexp="^([^,]*),"
select="\1"/>
对于您的第二个问题:Ant 属性是故意不可变的,因此我通常建议不要进行依赖于更改属性值的设计。但如果这是您需要的,var
Ant-Contrib 的任务允许您这样做。此外,Ant-Contrib 中的一些属性任务,如propertyregex
上面提到的,有一个可选override
属性,允许它们更改目标属性的值。
这是完成您所描述的一种方法。
<target name="parse" description="Example of how to parse the module.list property to extract the first value in the CSV">
<property name="module.list" value="mod1,mod2,mod3,mod4,mod5"/>
<tempfile description="Generate a unique temporary filename for processing"
prefix="module.list" suffix="csv" destdir="${basedir}" property="tmpFile" />
<concat description="Write the value of module.list to the temporary file"
destfile="${tmpFile}">${module.list}</concat>
<replaceregexp description="filter the temporary file using the regex expression to find the first occurance of a , and all characters afer and replace with nothing"
file="${tmpFile}"
match=",.*"
replace=""
byline="true"/>
<loadresource description="read the contents of the scrubbed temporary file into the mod1 property"
property="mod1">
<file file="${tmpFile}"/>
</loadresource>
<delete description="remove the temporary file"
file="${tmpFile}" />
<!--Now you have the parsed value in the mod1 property -->
<echo message="mod1=${mod1}" />
</target>
First question
With a new Ant addon = Flaka you may use =
<project xmlns:fl="antlib:it.haefelinger.flaka">
<property name="module.list" value="mod1,mod2,mod3,mod4,mod5"/>
<target name="main">
<!-- simple echo -->
<fl:echo>xtractedvalue => #{split('${module.list}',',')[0]}</fl:echo>
<!-- create property for further processing.. -->
<fl:let>
xtractedvalue := split('${module.list}',',')[0]
</fl:let>
<echo>$${xtractedvalue} => ${xtractedvalue}</echo>
</target>
</project>
Second question
normally properties are immutable once set in ant, but with Flaka you may overwrite an existing property like that =
<property name="foo" value="bar"/>
<fl:let>foo ::= 'baz'</fl:let>
would overwrite the existing property foo with new value baz.
使用脚本任务。您可以使用 Javascript 或 Beanshell 编写脚本,并使用 Ant API 设置您可以从其他 ant 任务访问的属性。