Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get number of lines in property using resourcecount

Tags:

java

ant

I am trying to get a ANT-Buildscript to count the lines that are stored inside a ANT-property. From the examples I got the way to count the lines in a file, like this:

<resourcecount count="0" when="eq">
    <tokens>
        <concat>
            <filterchain>
                <tokenfilter>
                    <linetokenizer/>
                </tokenfilter>
            </filterchain>
            <fileset file="${file}" />
        </concat>
    </tokens>
</resourcecount>

Now I want to refer to a ANT-property instead of the file. Is there a way to do this? I know about the solution to write the contents of the property into a file using <echo file="${temp.file}">${the.property.with.many.lines}</echo> and using the code above after. But I wonder if there is a solution that works without a temporary file.

like image 819
Nitram Avatar asked Jul 21 '12 19:07

Nitram


1 Answers

A propertyresource element may used in place of the fileset as follows:

<property name="lines"
    value="line01${line.separator}line02${line.separator}line03"/>

<target name="count-lines">
  <resourcecount property="line.count" count="0" when="eq">
    <tokens>
      <concat>
        <filterchain>
          <tokenfilter>
            <stringtokenizer delims="${line.separator}" />
          </tokenfilter>
        </filterchain>
        <propertyresource name="lines" />
      </concat>
    </tokens>
  </resourcecount>
  <echo message="${line.count}" />
</target>

Output

count-lines: [echo] 3

BUILD SUCCESSFUL Total time: 0 seconds

like image 78
Christopher Peisert Avatar answered Sep 20 '22 21:09

Christopher Peisert