tags:

views:

463

answers:

4

Given an ant fileset, I need to perform some sed-like manipulations on it, condense it to a multi-line string (with effectively one line per file), and output the result to a text file.

What ant task am I looking for?

+3  A: 

Try the ReplaceRegExp optional task.

ReplaceRegExp is a directory based task for replacing the occurrence of a given regular expression with a substitution pattern in a selected file or set of files.

There are a few examples near the bottom of the page to get you started.

Rich Seller
+1  A: 

Looks like you need a conbination of tasks:

This strips the '\r' and '\n' characters of a file and load it to a propertie:

<loadfile srcfile="${src.file}" property="${src.file.contents}">
  <filterchain>
    <filterreader classname="org.apache.tools.ant.filters.StripLineBreaks"/>
  </filterchain>
</loadfile>

After loading the files concatenate them to another one:

<concat destfile="final.txt">
  ...
</concat>

Inside concat use a propertyset to reference the files content:

<propertyset id="properties-starting-with-bar">
  <propertyref prefix="src.file"/>
</propertyset>
rodrigoap
That's very good ... I don't think you got all the details of the question down, but that shows me enough to make a full ant solution, if I want.I'm not manipulating the contents of files. I have a list of files built with a <fileset> tag; I want to apply an Ant <mapper> to the list (basically to strip off some directories and other transformations) and spit the whole thing out to a file, one per line.
skiphoppy
So given what you shared with me, I could echo the <fileset> to a file, load it back in with a <filterset>, and then write back out to a file. That'd be pure ant. I hate making the roundtrip to an extra temp file, though.
skiphoppy
ok, I get it know. Is not that bad to have an extra file ;)
rodrigoap
Yes, it simply is that bad to have an extra file. :) It's something I might choose to live with, but it's very messy! I learned the lesson of not leaving extra files lying around at every step 10 years ago in the UNIX shell, and I'd like to avoid it in ant.
skiphoppy
A: 

rodrigoap's answer is enough to build a pure ant solution, but it's not clean enough for me and would be some very complicated ant code, so I used a different method: I subclassed ant's echo task to make an echofileset task, which takes a fileset and a mapper. Subclassing echo buys me the ability to output to a file. A regexmapper performs the transformation on filenames that I need. I hardcoded it to print out each file on a separate line, but if I needed more flexibility I could add an optional separator attribute. I also thought about providing the ability to output to a property, too, but it turned out I didn't need it since I echo'ed straight to a file.

skiphoppy
+2  A: 

The Ant script task allows you to implement a task in a scripting language. If you have JDK 1.6 installed, Ant can execute JavaScript without needing any additional dependent libraries. The JavaScript code can read a fileset, transform the file names, and write them to a file.

  <fileset id="jars" dir="${lib.dir}">
    <include name="*.jar"/>
  </fileset>

  <target name="init">
    <script language="javascript"><![CDATA[
        var out = new java.io.PrintWriter(new java.io.FileWriter('jars.txt'));

        var iJar = project.getReference('jars').iterator();
        while (iJar.hasNext()) {
            var jar = new String(iJar.next());
            out.println(jar);
        }

        out.close();
    ]]></script>
  </target>
Jim Huang
Jim, that is awesome! Didn't see that before I started subclassing ant tasks. I had seen something similar for Groovy, but didn't want to incorporate Groovy into our project just for this.
skiphoppy