File Read Write using Ant Script - ant

I have 2 files:
Read.txt:
My name is=Joy
My age is=18
Write.txt:
Paste my name here:
Paste my age here:
So I should be able to read the name(Joy) and age(18) from Read.txt from the desired location(it will appear after '=') and write them into write.txt at the desired location(after ':') after a successful build. Do I need regex for this?
Can we do this using an Ant Script?

With invalid property names in your read.txt file, you'll have to do something a bit more complex.
Using the loadfile task, you can load the entire contents of the file and format it as necessary:
<target name="write-to-file">
<loadfile srcfile="read.txt" property="output">
<filterchain>
<tokenfilter>
<replacestring from="My name is=" to="Paste my name here:" />
<replacestring from="My age is=" to="Paste my age here:" />
</tokenfilter>
</filterchain>
</loadfile>
<echo file="write.txt" message="${output}" />
</target>
If you need to get the specific values for name and age (perhaps for use later in the script) you will have to load twice and filter the parts you need:
<target name="load-lines">
<loadfile srcfile="read.txt" property="my.name">
<filterchain>
<linecontains>
<contains value="My name is" />
</linecontains>
<tokenfilter>
<replacestring from="My name is=" to="" />
</tokenfilter>
</filterchain>
</loadfile>
<loadfile srcfile="read.txt" property="my.age">
<filterchain>
<linecontains>
<contains value="My age is" />
</linecontains>
<tokenfilter>
<replacestring from="My age is=" to="" />
</tokenfilter>
</filterchain>
</loadfile>
<echo file="write.txt" message="Paste my name here:${my.name}${line.separator}Paste my age here:${my.age}" />
</target>

Related

Find the number of occurrences of a particular character like comma in a string using ant script?

<project>
<target name="test">
<property name="src.dir" value="src" />
<property name="search4" value=","/>
<fileset id="existing" dir="${src.dir}/src">
<patternset id="files">
<include name="*.txt"/>
</patternset>
</fileset>
<resourcecount property="count">
<fileset id="matches" dir="../src">
<patternset refid="files" />
<contains text="${search4}" />
</fileset>
</resourcecount>
<echo message="Found '${search4}' in files : '${count}'"/>
</target>
</project>
I used this, but this only prints the first occurrence. I would like to print the total count.
For eg - abc,xyz,pg--> The number of occurrences of commas(,) is 2.
Here's one way. Copies the file to another file, with a filter to remove all non-commas, then gets the size of the output, which is the number of commas in the input file.
<delete file="out.txt" />
<copy file="in.txt" tofile="out.txt">
<filterchain>
<striplinebreaks />
<replaceregex pattern="[^,]" replace="" flags="gm" />
</filterchain>
</copy>
<length file="out.txt" property="out.size" />
<echo message="Commas found: ${out.size}" />
On your follow up question: how to restrict this to just the first line of the file: add this before the "striplinebreaks" line:
<headfilter lines="1" />
That will count commas in just the first line of the file.

How to solve this ANT issue?

<loadfile property="UIfiles" srcfile="updated.txt">
<filterchain>
<linecontainsregexp>
<regexp pattern="ui/dev/"/>
</linecontainsregexp>
</filterchain>
</loadfile>
<echo file="Filelist.txt" append="true">${UIfiles}</echo>
I have the above code in build.xml file. updated.txt will contain some text like Projects/accounts/spec/ui/dev/dpdl/abc.xml. If this statement is present in the file, then the above code works as expected. If there is no match for regex "ui/dev" in updated.txt, ideally the value of UIfiles should be empty and should not write anything to Filelist.txt. But in my case "${UIfiles}" is getting appended in Filelist.txt. Please suggest how to avoid this. Thank you.
Works as expected. ${...} is the syntax for your property when not set, because
your file doesn't contain a line matching the regexp.
You need some if isset condition, with Ant 1.9.3 and new if unless feature :
<project
xmlns:if="ant:if"
xmlns:unless="ant:unless"
>
<loadfile property="UIfiles" srcfile="updated.txt">
<filterchain>
<linecontainsregexp>
<regexp pattern="ui/dev/"/>
</linecontainsregexp>
</filterchain>
</loadfile>
<echo file="Filelist.txt" append="true">${UIfiles} if:set="UIfiles"</echo>
</project>
otherwise for older Ant versions use:
<project>
<target name="checkfile">
<loadfile property="UIfiles" srcfile="updated.txt">
<filterchain>
<linecontainsregexp>
<regexp pattern="ui/dev/"/>
</linecontainsregexp>
</filterchain>
</loadfile>
</target>
<target name="appendfilelist" depends="checkfile" if="UIfiles">
<echo file="Filelist.txt" append="true">${UIfiles}</echo>
</target>
<project>

Retrieve all keys from properties file using Ant

I have a properties file containing key/value pairs:
key1=value1
key2=value2
...
How can I retrieve a list of all keys in this file using Ant?
Use loadfile with a filterchain, f.e. :
<project>
<!-- given some file with :
key=value
key=someothervalue
...
-->
<loadfile property="keysonly" srcfile="some.properties">
<filterchain>
<tokenfilter>
<replaceregex pattern="(.+)=.+" replace="\1"/>
</tokenfilter>
</filterchain>
</loadfile>
<echo>${keysonly}</echo>
</project>
If you need the keys in special form, f.e. comma separated use something like :
<loadfile property="keysonly" srcfile="some.properties">
<filterchain>
<tokenfilter>
<!-- use some delimiter f.e. '###' -->
<replaceregex pattern="(.+)=.+" replace="\1###"/>
</tokenfilter>
<!-- get rid of linefeeds -->
<striplinebreaks/>
<tokenfilter>
<!-- replace delimiter '###' with ',' -->
<replaceregex pattern="###" replace="," flags="g"/>
</tokenfilter>
<tokenfilter>
<!-- replace dangling ',' -->
<replaceregex pattern=",$" replace=""/>
</tokenfilter>
</filterchain>
</loadfile>

Want to remove [apply], [script] like keywords from file

I am using following code block:
<copy tofile="${dir.report}\${file.report.name}.html" file="${dir.report}\${file.report.name}.html">
<filterchain>
<tokenfilter>
<replaceregex pattern="\[(script|apply)\]" replace="" />
</tokenfilter>
</filterchain>
</copy>
But replaceregex is not working
Can someone help me out here.
This would be a simpler solution:
<copy tofile="build/input1.html" file="src/input1.html">
<filterchain>
<tokenfilter>
<replacestring from="apply" to=""/>
<replacestring from="script" to=""/>
</tokenfilter>
</filterchain>
</copy>
The <copy> task cannot "self-copy" files. The source file path has to be different than the destination file path.
Luckily, the <replaceregexp> task provides a simpler solution:
<replaceregexp file="${dir.report}\${file.report.name}.html" flags="g">
<regexp pattern="\[(script|apply)\]"/>
<substitution expression=""/>
</replaceregexp>

Ant: get multiple matches with propertyregex

I have these (sample) lines in a HTML-file:
test.ABC.test
test.ABCD.test
test.ABCE.test
And this Ant propertyregex:
<loadfile property="getRecords" srcFile="./index.html"/>
<propertyregex property="record" input="${getRecords}" regexp="test\.([^\.]*)\.test" select="\1" casesensitive="true" override="true" global="true" />
<echo message="${record}" />
The result is just
ABC
But I'd like to get all matches. How can I get
ABC
ABCD
ABCE
as result?
Not sure about the propertyregex problem, but this works (without ant-contrib):
<target name="test">
<loadfile property="record" srcFile="./index.html">
<filterchain>
<tokenfilter>
<containsregex pattern=".*test\.([^\.]*)\.test.*" replace="\1"/>
</tokenfilter>
</filterchain>
</loadfile>
<echo message="${record}" />
</target>

Resources