It is enough to print a list of lines separated by commas in ant

I have a comma delimited string in the ant property, for example:

<property name="prop" value="a,b,c"/> 

I would like to be able to print or write it like this:

 Line 1: a Line 2: b Line 3: c 

It doesn't seem like this should be too complicated, but I can't figure out which ant components I should put together.

+4
source share
2 answers

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> 

Output:

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> 

Output:

Ant properties
Tue Nov 22 17:12:55 2011
Line_0 = a
Line_1 = b
Line_2 = s

+4
source

You can do this using loadresource by specifying the value of the property as string . Now you can use the replaceregex filter to convert the comma to a new line.

 <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> 

Output:

 test: [echo] a [echo] b [echo] c 
+9
source

Source: https://habr.com/ru/post/1382463/


All Articles