Text manipulation in ant

Given the set of ant files, I need to do some sed-like manipulations on it, condense it to a multi-line line (effectively with one line per file) and output the result to a text file.

What ant task am I looking for?

+4
source share
4 answers

It looks like you need to combine tasks:

This separates the characters '\ r' and '\ n' of the file and loads it into the file:

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

After downloading, the files merge them into another:

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

Inside concat, use propertyset to reference the contents of files:

 <propertyset id="properties-starting-with-bar"> <propertyref prefix="src.file"/> </propertyset> 
+1
source

The Ant script task allows you to implement the task in a scripting language. If you have JDK 1.6 installed, Ant can execute JavaScript without any additional dependent libraries. JavaScript code can read a set of files, convert 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> 
+5
source

Try ReplaceRegExp optional task.

ReplaceRegExp is a directory-based task to replace the appearance of a given regular expression with a lookup pattern in a selected file or set of files.

At the bottom of the page there are some examples to get started.

+3
source

rodrigoap answer is enough to create a clean ant solution, but it is not enough for me and it will be very complex ant code, so I used another method: I subclassed ant echo to perform the echofileset task, which takes up a set of files and a mapper . Subclassing echo buys the ability to output to a file. regexmapper performs the conversion on the file names that I need. I programmed it hard to print each file on a separate line, but if I needed more flexibility, I could add an optional separator attribute. I also thought about allowing output to the property, too, but it turned out that I didn’t need it, since I echoed directly to the file.

0
source

All Articles