1

I want to prepend each matching line with file name where it's found.

I tried ${file.name}, but it does not work.

<concat destFile="report.xml">
    <filterchain>
        <linecontainsregexp>
            <regexp pattern="somePattern"/>
        </linecontainsregexp>
        <prefixlines prefix="${file.name}"/>
    </filterchain>
    <fileset dir="${pathToDirectory}" erroronmissingdir="false">
        <include name="**"/>
    </fileset>
</concat>
Siddharth Thevaril
  • 3,722
  • 3
  • 35
  • 71
ptkvsk
  • 2,096
  • 1
  • 25
  • 47

1 Answers1

1

The following code uses the <for> task from the third-party Ant-Contrib library. <for> iterates over every file in a fileset.

<for param="file.path">
  <path>
    <fileset dir="${pathToDirectory}" erroronmissingdir="false">
      <include name="**"/>
    </fileset>
  </path>
  <sequential>
    <concat destFile="report.xml" append="true">
      <path>
        <pathelement location="@{file.path}"/>
      </path>
      <filterchain>
        <linecontainsregexp>
          <regexp pattern="somePattern"/>
        </linecontainsregexp>
        <prefixlines prefix="@{file.path}:"/>
      </filterchain>
    </concat>
  </sequential>
</for>

To use Ant-Contrib, download ant-contrib-1.0b3-bin.zip, extract ant-contrib-1.0b3.jar from it, and follow the instructions on how to install Ant-Contrib.

Community
  • 1
  • 1
Chad Nouis
  • 6,861
  • 1
  • 27
  • 28
  • One thing I have to add: append="true" is mandatory with this solution, but it means that when you run build twice, the file will have duplicate output. To avoid this, you need to add before – ptkvsk Aug 14 '15 at 20:13