Text manipulation in ant - ant

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?

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>

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.

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'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.

Related

How to acess property within a property in ant

Hi all please give a look to this code
in my properties file i have
win-x86.pc-shared-location=E:\Ant_Scripts
Now below i am trying to call PrintInstallerName_build from my build.xml,while as PrintInstallerName_build is in test.xml. In build.xml file,${platform.id} has value=win-x86 in the calling target and in called target param1 also has value=win-x86
<target name="PrintInstallerName" >
<echo>PlatForm.Id====>${platform.id}</echo>
<ant antfile="test.xml" target="PrintInstallerName_build">
<property name="param1" value="${platform.id}"/>
</ant>
<target name="PrintInstallerName_build" >
<echo>${param1.pc-shared-location}</echo><!--${param1.pc-shared-location}-->
<echo>${param1}.pc-shared-location}</echo><!--win-x86.pc-shared-location-->
<echo>${win-x86.pc-shared-location}</echo><!--E:\\Ant_Scripts-->
</target>
as you can see only the last statement gives correct output but it is hardcoded,i want to use param1 and the output should be E:\\Ant_Scripts i tried to use $ and # but none works,may be i am doing wrong somewhere can someone help please,i am struck and tomorrow is its DOD.
See Nesting of Braces in the Properties page of the Ant Manual.
In its default configuration Ant will not try to balance braces in
property expansions, it will only consume the text up to the first
closing brace when creating a property name. I.e. when expanding
something like ${a${b}} it will be translated into two parts:
the expansion of property a${b - likely nothing useful.
the literal text } resulting from the second closing brace
This means you can't use easily expand properties whose names are
given by properties, but there are some workarounds for older versions
of Ant. With Ant 1.8.0 and the the props Antlib you can configure Ant
to use the NestedPropertyExpander defined there if you need such a
feature.
You can use <propertycopy> to make it happen.
Consider that you need to have the property value of ${propA${propB}}
Use ant tag of propertycopy as follows:
<propertycopy property="myproperty" from="PropA.${PropB}"/>
<echo >${myproperty}</echo>
This will echo the value of ${propA${propB}}
<target name="PrintInstallerName_process" >
<echo>${param1}</echo><!--win-x86-->
<macrodef name="testing">
<attribute name="v" default="NOT SET"/>
<element name="some-tasks" optional="yes"/>
<sequential>
<echo>Source Dir of ${param1}: ${#{v}}</echo><!-- Dir of Win-x86:E:\Ant_Scripts-->
<some-tasks/>
</sequential>
</macrodef>
<testing v="${param1}.pc-shared-location">
<some-tasks>
</some-tasks>
</testing>
</target>
this is the way it works and for me it works fine anyways #sudocode your tip took me there so thank you very much

Find all directories in which a file exists, such that the file contains a search string

I have a directory tree that I need to process as follows:
I have a certain file that needs to be copied to a select few sub directories
A sub directory of interest is one that contains a file within which I can regex match a known search string
Ideally I would like to:
Perform a regex match across all files within a directory
If the regex matches, copy the file to that directory
The trouble is that I am quite new to ANT and I'm having difficulties finding my way around. I can't find any tasks in the docs about per directory operations based on regex search. The closest thing I've found is a regex replace task (<replaceregexp>) that can search and replace patterns across files.
Is this even possible? I'd really appreciate a sample to get started with. I apologize for requesting code - I simply don't know how to begin composing the tasks together to achieve this.
Alternatively I have the option of hardcoding all the copy operations per directory, but it would mean manually keeping everything in sync as my project grows. Ideally I'd like to automate it based on the regex search/copy approach I described.
Thanks!
Your requirement is a bit non-standard, so I've solved it using a custom Groovy task.
Here's a working example:
<project name="find-files" default="copy-files">
<!--
======================
Groovy task dependency
======================
-->
<path id="build.path">
<pathelement location="jars/groovy-all-1.8.6.jar"/>
</path>
<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="build.path"/>
<!--
=========================
Search for matching files
=========================
-->
<target name="search-files">
<fileset id="filesContainingSearchString" dir="src">
<include name="**/*.txt"/>
<containsregexp expression="[4-6]\.[0-9]"/>
</fileset>
</target>
<!--
===================================
Copy file into each directory found
===================================
-->
<target name="copy-files" depends="search-files">
<groovy>
project.references.filesContainingSearchString.each { file ->
def dir = new File(file.toString()).parent
ant.copy(file:"fileToBeCopied.txt", toDir:dir)
}
</groovy>
</target>
</project>
Notes:
Groovy jar can be downloaded from Maven Central
Use the copy task with a fileset and regular expression selector :
<copy todir="your/target/dir">
<fileset dir="rootdir/of/your/directorytree" includes="**/*.txt">
<containsregexp expression="[4-6]\.[0-9]"/>
</fileset>
</copy>
This example is taken from the ant manual and slightly adapted.
Means select all files with .txt extension anywhere beyond rootdir/of/your/directorytree that match the regular expression (have a 4,5 or 6 followed by a period and a number from 0 to 9) and copy them to your/target/dir.
Just adapt it for your needs.

ant copy from absolute path read from xml

My problem is, I have to read the source path for a copy job from an xml file and then copy all files in that dir read from the xml file to another dir.
Since code is more than words:
<xmltask source="${projectfile}">
<copy path="Project/RecentResultsInfo/ResultsDirectoryOfRecentLoadTest/text()" property="recentdir" attrValue="true"/>
</xmltask>
<copy todir="${targetdirectory}">
<fileset dir="${recentdir}"/>
</copy>
The output when running this target is:
C:\develop\build.xml:44: Warning: Could not find resource file "C:\develop\C:\Programme\tool\test_90\" to copy.
It seems in fileset it does not recognize, that recentdir holds a full path inside. The written xml from the application has a newline before and after the path in the xml file that is read with the path. So ant does not recognize the path since theres a newline in front of it.
Is there anything like trim for ant?
Can anybody help me getting ant to accept that path?
Done it now by using Ant-Contrib, but that is used in this project anyway.
<xmltask source="${projectfile}">
<copy path="Project/RecentResultsInfo/ResultsDirectoryOfRecentLoadTest/text()" property="recentdirraw" attrValue="true"/>
</xmltask>
<!-- replace newlines and whitespace from read path -->
<propertyregex property="recentdir" input="${recentdirraw}" regexp="^[ \t\n]+|[ \t\n]+$" replace="" casesensitive="false" />
<copy todir="${targetdirectory}">
<fileset dir="${recentdir}"/>
</copy>
Simply modifying the property with a regex trimming the text by striping of whitespace and newlines.
As far as I can see, the copy element in xmltask provides a trim attribute.
trims leading/trailing spaces when writing to properties
Does that work?

editing the xml using ant task

I was trying to edit the my config.xml file using ant task but I could not do that can anybody tell me How can I edit the xml using ant task automatically so that i dont need to change it manually for every new branch?
The first option to check would be the Ant xslt task. For an introduction to its use see the Ant/XSLT Wikibook.
I've used groovy to do this. groovy is very Java like, so you can create your groovy classes very similarly to a static java method, and have ant call out to your groovy script using the <groovy> task (you will of course need to include the groovy task def).
Because groovy can use Java syntax you can include the org.w3c.com.* libraries to have access to DOM classes.
For example, snippet of code showing the adding a resource ref element to a specifed web.xml file :-
import org.w3c.dom.*;
String web_xml_filename=args[0];
String res_ref_name=args[1];
Document doc = DomHelper.getDoc(web_xml_filename);
Element rootNode=doc.getDocumentElement();
newNode = doc.createElement("resource-ref");
DomHelper.createElement(doc, newNode, "res-ref-name", res_ref_name);
DomHelper.createElement(doc, newNode, "res-type", "javax.sql.DataSource");
DomHelper.createElement(doc, newNode, "description", description);
DomHelper.createElement(doc, newNode, "res-auth", "Container");
rootNode.insertBefore(newNode, nodes.item(0));
DomHelper.writeDoc(doc, web_xml_filename, false);
To call from ant, use the groovy task :-
<groovy src="${e5ahr-groovy.dir}/addResoureRefToJBossWebXML.groovy" classpath="${groovy.dir}">
<arg value="${jboss-web.xml}"/>
<arg value="jdbc/somesource/>
<arg value="java:jdbc/somesource"/>
</groovy>
You can use ReplaceRegExp. Pattern and Expression options won't let you use less than or more than, but it can be replaced with HTML entities. For example:
<replaceregexp byline="true">
<!-- In config.xml this looks like <myVariable></myVariable> -->
<regexp pattern="<myVariable>(.*)</myVariable>" />
<substitution expression="<myVariable>${myVariable.value}</myVariable>" />
<fileset dir="${user.dir}">
<include name="config.xml" />
</fileset>
</replaceregexp>

Can I send Ant 'replace' task output to a new file?

The Ant replace task does an in-place replacement without creating a new file.
The below snippet replaces tokens in any of the '*.xml' files with the corresponding values from the 'my.properties' file.
<replace dir="${projects.prj.dir}/config"
replacefilterfile="${projects.prj.dir}/my.properties"
includes="*.xml" summary="true" />
I want those files that had their tokens replaced to be created named after a pattern (e.g.) '*.xml.filtered', and keep the original files.
Is this possible in Ant with some smart combination of tasks?
There are a couple of ways to get close to what you want without copying to a temporary directory and copying back.
Filtersets
If the source files can be changed so that the parts to be replaced can be delimited with begin and end tokens, as in #date# (# is the default token, but it can be changed) then you can use the copy task with a globmapper and a filterset:
<copy todir="config">
<fileset dir="config" includes="*.xml" />
<globmapper from="*.xml" to="*.xml.filtered" />
<filterset filtersfile="replace.properties" />
</copy>
If replace.properties contains FOO = bar, then any occurrence of #FOO# in a source xml file file be replaced with bar in the target.
Note that the source and target directories are the same, the globmapper means the target files and named with the suffix .filtered. It's possible (and more usual) to copy files into a different target directory)
Filterchains
If the source file can't be changed to add begin and end tokens, a possible alternative would be to use a filterchain with one or more replacestring filters instead of the filterset:
<copy todir="config">
<fileset dir="config" includes="*.xml" />
<globmapper from="*.xml" to="*.xml.filtered" />
<filterchain>
<tokenfilter>
<replacestring from="foo" to="bar" />
<!-- extra replacestring elements here as required -->
</tokenfilter>
</filterchain>
</copy>
This will replace any occurrence of foo with bar, anywhere in the file, which is more like the behaviour of the replace task. Unfortunately this way means you need to include all your replacements in the build file itself, you can't have them in a separate properties file.
In both cases the copy task will only copy source files that are newer than the target files, so unnecessary work won't be done.
Copy then replace
A third possibility (that has just occured to me whilst writing up the other two) would be to perform the copy first to the renamed files, then run the replace task specifying the renamed files:
<copy todir="config">
<fileset dir="config" includes="*.xml" />
<globmapper from="*.xml" to="*.xml.filtered" />
</copy>
<replace dir="config" replacefilterfile="replace.properties" summary="true"
includes="*.xml.filtered" />
This might be the closest solution to the original requirement. The downside is that the replace task will be run each time on the renamed files. This could be a problem for some replacement patterns (admittedly they would be odd ones like foo=foofoo, but they would be okay with the first two methods) and you will be doing unnecessary work when the dependencies don't change.
The replace task doesn't observe dependencies, instead it carries out the replacement by writing a temporary file for each input file. If the temporary file is the same as the input file, it is discarded. A temporary file that differs from the input file is renamed to replace that input. This means all the files are processed, even if none of them need be - hence it can be inefficient.
The original solution to this question was to carry out a copy-replace-copy. The second copy isn't needed though, as a mapper can be used in the first. In the copy, dependencies can be used to restrict processing to just the files that have changed - by means of a depend selector in an explicit fileset:
<copy todir="${projects.prj.dir}">
<fileset dir="${projects.prj.dir}">
<include name="*.xml" />
<depend targetdir="${projects.prj.dir}">
<mapper type="glob" from="*.xml" to="*.xml.filtered" />
</depend>
</fileset>
<mapper type="glob" from="*.xml" to="*.xml.filtered" />
</copy>
That will restrict the copy fileset to just those files that have changed. An alternative syntax for the mappers is:
<globmapper from="*.xml" to="*.xml.filtered" />
The simplest replace would then be:
<replace dir="${projects.prj.dir}"
replacefilterfile="my.properties"
includes="*.xml.filtered" />
That will still process all the files though, even if none of them need undergo replacements. The replace task has an implicit fileset and can operate on an explicit fileset, but unlike similar tasks the implicit fileset is not optional, hence to take advantage of selectors in an explicit fileset you must make the implicit one 'do nothing' - hence the .dummy file here:
<replace dir="${projects.prj.dir}"
replacefilterfile="my.properties">
includes=".dummy" />
<fileset dir="${projects.prj.dir}" includes="*.xml.filtered">
<not>
<different targetdir="${projects.prj.dir}">
<globmapper from="*.xml.filtered" to="*.xml" />
</different>
</not>
</fileset>
</replace>
That will prevent the replace task from needlessly processing files that have previously undergone substitution. It doesn't, however, prevent processing of files that haven't changed and don't need substitution.
Beyond that, I'm not sure there is a way to 'code golf' this problem to reduce the number of steps to one.
There isn't a multiple string replacement filter that can be used in a copy task to achieve the same affect as replace, which is a shame because that feels like it would be the right solution.
One other approach would be to generate the xml for a series of replace string filters and then have Ant execute that. But that will be more complex than the existing solution, and prone to problems with replacement strings that, if pasted into an xml fragment will result in something that can't be parsed.
Yet another approach would be to write a custom task or script task to do the work. If there are many files and the copy-replace solution is judged to be too slow, then this might be the way to go. But again, that approach is less simple than the existing solution.
If the requirement is to minimise the work done in the processing, rather than to come up with the shortest Ant solution, then this approach might do.
Make a fileset containing a list of inputs that have changed.
From that fileset create a comma-separated list of corresponding filtered files.
Carry out the copy on the fileset.
Carry out the replace on the comma-separated list.
A wrinkle here is that the implicit fileset in the replace task will fall back to processing everything if no files have changed. To overcome this we insert a dummy file name.
<fileset id="changed" dir="${projects.prj.dir}" includes="*.xml">
<depend targetdir="${projects.prj.dir}">
<globmapper from="*.xml" to="*.xml.filtered" />
</depend>
</fileset>
<pathconvert property="replace.includes" refid="changed">
<map from=".xml" to=".xml.filtered" />
</pathconvert>
<copy todir="${projects.prj.dir}" preservelastmodified="true">
<fileset refid="changed" />
<globmapper from="*.xml" to="*.xml.filtered" />
</copy>
<replace dir="${projects.prj.dir}"
replacefilterfile="my.properties"
includes=".dummy,${replace.includes}" summary="true" />

Resources