Append to the end of a line in an Ant Build File - ant

So i have an ant build file which needs to modify this other file named modify.properties. In this properties file i have a line such as this -
CSDT_FLAG_CSELEMENT_FWUIDS=CSElement:4f826165-4744-4d78-63f0-4f437eb17872,b2db8c3d-ec81-4c6e-9425-ff0860f4511b,13f5a17a-8dac-443b-bcf8-b1675e660c53
In my build file I will search for the term CSElement and get the line that contains this term
<loadfile
property="csdtFlagPropertiesFile"
srcFile="../cms-distribution/src/main/x/csdtflags.properties">
<filterchain>
<filterreader classname="org.apache.tools.ant.filters.LineContainsRegExp">
<param type="regexp" value="CSElement"/>
</filterreader>
</filterchain>
</loadfile>
Now i have the line CSDT_FLAG_CSELEMENT_FWUIDS=CSElement:4f826165-4744-4d78-63f0-4f437eb17872,b2db8c3d-ec81-4c6e-9425-ff0860f4511b,13f5a17a-8dac-443b-bcf8-b1675e660c53 and i can replace the line using the containsregex method in filterchain however i am unable to write this back onto the file. A message such as echo message will rewrite the entire file but i just want to replace that line with another line. replacing works but this saves it into a property and i dont know how i can rewrite this on the file. Any help much appreciated.

You could use the ReplaceRegExp task to make a substitution in the file.

Related

Is there a way to make #suffix on task basename in Apache Ant case-insensitive?

in my build file, I have declared:
<basename property="filename" file="${args.input}" suffix="XML"/>
where ${args.input} is passed in through an Oxygen transformation scenario, ex -Dargs.input="${cfd}\PMC-min.XML"
${filename} returns PMC-min, which is the desired output. I want the file name without any extension.
However, after discovering an error I realized that
<basename property="filename" file="${args.input}" suffix="xml"/>
was returning PMC-min.XML. So #suffix is case-sensitive.
I could change ${args.input} to ${cfd}\PMC-min (which would require other changes to the build file), or just make sure the extension case of ${args.input} matches #suffix in the scenario. But I was wondering if there was a case-insensitive way to retrieve the filename without the extension in ant? (It doesn't seem to matter if the case of the actual file's extension is different, only the parameters have to match).
One way that might work for you is to use a <mappedresources> "collection of one", something like this:
<mappedresources id="converted">
<string value="${args.input}" />
<chainedmapper>
<flattenmapper />
<globmapper casesensitive="false" from="*.xml" to="*" />
</chainedmapper>
</mappedresources>
<echo message="file_name=${toString:converted}" />
The echo is just to illustrate how to reference the resource.
In the above:
the string resource specifies the input value
the chained mapper combines two mapping steps to alter the string:
a flattenmapper removes the directory part
a globmapper removes the suffix, ignoring case.

delete all lines from text except first using ANT

Is there a way to delete all the text from a text file using ANT except the first line?
Example:
First line
Second line
.....
Line n
I want to delete everything except first line. The lines can contain any text.
Use <loadfile> with a <headfilter> to get just the first line of a file. Then, the original file can be overwritten using the file attribute of <echo>.
<!-- Use headfilter to save the file's first line in a property. -->
<loadfile srcfile="${text.file}" property="first.line">
<filterchain>
<headfilter lines="1"/>
</filterchain>
</loadfile>
<!-- Overwrite the file with only the first line. -->
<echo file="${text.file}">${first.line}</echo>
test.txt before running Ant
First line
Second line
.....
Line n
test.txt after running Ant
First line

Ant Script for converting multiple file formats using mappers- creating double extension files

I want to convert multiple file formats to a single file format.
Example: D:\myrepo\rough has 3 files
1. abc.sql
2. def.xml
3. ghi.dmp
I want them all to be converted to .txt using glob mappers.
<?xml version ="1.0"?>
<project name = "roughone" default="taget1">
<target name= "target1">
<move todir="D:\myrepo\rough">
<fileset dir="D:\myrepo\rough">
</fileset>
<mapper type ="glob" from="*" to="*.txt"/>
</move>
</target>
</project>
This is giving
1. abc.sql.txt
2. def.xml.txt
3. ghi.dmp.txt where as i need only abc.txt,def.txt and ghi.txt.
Plz let me know how this can be fixed(from= "." is not helping too).
Replace your globmapper with the following <regexpmapper>:
<regexpmapper from="^(.*)[.][^.]+$$" to="\1.txt"/>
The above regular expression captures the part of each file name before the final period. The regular expression also discards whatever extension the file previously had.
The double "$$" is needed because Ant would interpret a single "$" as the beginning of a property reference.

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?

Text manipulation in 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.

Resources