如何从Ant中的for循环生成逗号分隔的列表?
我可以使用ant-contrib for循环遍历源属性,但我不确定如何输出到目标属性:
<ac:for list="${comma_seperated}" param="entry">
<sequential>
<if>
<isset property="@{entry}_enabled" />
<then>
<!-- append to property enabled_list here -->
</then>
</if>
</sequential>
</ac:for>
使用Ant Addon Flaka遍历csv属性并在每次迭代中添加内容的代码片段:
<project xmlns:fl="antlib:it.haefelinger.flaka">
<property name="foobar" value="Alpha,Beta,Gamma,Delta,Epsilon,Zeta,Eta,Theta,Iota,Kappa,Lambda"/>
<property name="foobaz" value="My,Ny,Xi,Omikron,Pi,Rho,Sigma,Tau,Ypsilon,Phi,Chi,Psi,Omega"/>
<fl:for var="item" in="split('${foobaz}', ',')">
<fl:let>foobar ::= concat('${foobar}',',#{item}')</fl:let>
</fl:for>
<echo>$${foobar} => ${foobar}</echo>
</project>
输出:[echo] ${foobaz} => Alpha,Beta,Gamma,Delta,Epsilon,Zeta,Eta,Theta,Iota,Kappa,Lambda,My,Ny,Xi,Omikron,Pi,Rho,Sigma,Tau,Ypsilon,Phi,Chi,Psi,Omega
EDIT
很高兴你明白了这个想法,尽管我的代码片段有点愚蠢,因为一个人会实现有些结果仅仅是
<property name=someproperty value=${foobar},${foobaz}>
不知道您的需求的细节,但这里有一个改编的片段,添加/length后缀到您的CSV属性的任何项
:
<project xmlns:fl="antlib:it.haefelinger.flaka">
<property name="foobar" value="Alpha,Beta,Gamma,Delta"/>
<property name="foobaz" value=""/>
<fl:for var="item" in="split('${foobar}', ',')">
<fl:choose>
<!-- avoid a leading ',' -->
<fl:when test="'${foobaz}'.length eq 0">
<fl:let>foobaz ::= concat('${foobaz}','#{item}/length#{item.length}')</fl:let>
</fl:when>
<fl:otherwise>
<fl:let>foobaz ::= concat('${foobaz}',',#{item}/length#{item.length}')</fl:let>
</fl:otherwise>
</fl:choose>
</fl:for>
<!-- optionally overwrite ${foobar} with ${foobaz} -->
<fl:let>foobar ::= '${foobaz}'</fl:let>
<echo>$${foobar} => ${foobar}</echo>
</project>
输出:[echo] ${foobar} => Alpha/length5,Beta/length4,Gamma/length5,Delta/length5
尝试使用标准Ant任务<pathconvert>
。参见http://ant.apache.org/manual/Tasks/pathconvert.html。