I have a comma-separated string in an ant property, like this:
<property name="prop" value="a,b,c"/>
I would like to be able to print or log it like this:
Line 1: a
Line 2: b
Line 3: c
Doesn't sound like it should be too difficult, but I cannot figure out which ant components I should be putting together.
You can do this by using loadresource specifying the property value as a string resource. Now you can use a replaceregex filter to convert comma to newline.
<project default="test">
<property name="prop" value="a,b,c"/>
<target name="test">
<loadresource property="prop.fmt">
<string value="${prop}"/>
<filterchain>
<tokenfilter>
<replaceregex pattern="," replace="${line.separator}" flags="g"/>
</tokenfilter>
</filterchain>
</loadresource>
<echo message="${prop.fmt}"/>
</target>
</project>
The output is:
test:
[echo] a
[echo] b
[echo] c
The sample using Ant-Contrib Tasks
<taskdef resource="net/sf/antcontrib/antlib.xml"/>
<target name="test_split">
<property name="prop" value="a,b,c"/>
<for list="${prop}" param="letter">
<sequential>
<echo>@{letter}</echo>
</sequential>
</for>
</target>
The output is:
a
b
c
Another solution from here:
<scriptdef name="split" language="javascript">
<attribute name="value"/>
<attribute name="delimiter"/>
<attribute name="prefix"/>
<![CDATA[
values = attributes.get("value").split(attributes.get("delimiter"));
for(i=0; i<values.length; i++) {
project.setNewProperty(attributes.get("prefix")+i, values[i]);
}
]]>
</scriptdef>
<target name="test_split2">
<property name="prop" value="a,b,c"/>
<property name="prefix_str" value="Line_"/>
<split value="${prop}" delimiter="," prefix="${prefix_str}"/>
<echoproperties prefix="${prefix_str}"/>
</target>
The output is:
Ant properties
Tue Nov 22 17:12:55 2011
Line_0=a
Line_1=b
Line_2=c
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With