Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pretty-printing comma-separated list of strings in ant

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.

like image 912
JesperE Avatar asked Dec 10 '22 04:12

JesperE


2 Answers

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
like image 184
sudocode Avatar answered Jan 17 '23 01:01

sudocode


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

like image 22
Alex K Avatar answered Jan 17 '23 01:01

Alex K