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

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>

Related

Ant - how to print file name from fileset in a filterchain

I want to prepend each matching line with file name where it's found.
I tried ${file.name}, but it does not work.
<concat destFile="report.xml">
<filterchain>
<linecontainsregexp>
<regexp pattern="somePattern"/>
</linecontainsregexp>
<prefixlines prefix="${file.name}"/>
</filterchain>
<fileset dir="${pathToDirectory}" erroronmissingdir="false">
<include name="**"/>
</fileset>
</concat>
The following code uses the <for> task from the third-party Ant-Contrib library. <for> iterates over every file in a fileset.
<for param="file.path">
<path>
<fileset dir="${pathToDirectory}" erroronmissingdir="false">
<include name="**"/>
</fileset>
</path>
<sequential>
<concat destFile="report.xml" append="true">
<path>
<pathelement location="#{file.path}"/>
</path>
<filterchain>
<linecontainsregexp>
<regexp pattern="somePattern"/>
</linecontainsregexp>
<prefixlines prefix="#{file.path}:"/>
</filterchain>
</concat>
</sequential>
</for>
To use Ant-Contrib, download ant-contrib-1.0b3-bin.zip, extract ant-contrib-1.0b3.jar from it, and follow the instructions on how to install Ant-Contrib.

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>

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>

Replace property in a xml file using ANT

I'm trying to replace a version number in a build.xml file using an ANT script.
I've tried various approaches, searched and re-searched StackOverflow for answers but could not get the exact query.
so here is my xml file:
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.0"?>
<project name="feature" default="main" basedir=".">
<target name="init">
<property name="Version" value="1.0.0.20120327"/>
</target>
<target name="main" depends="init">
<description>Main target</description>
</target>
</project>
Now as u can see the Version has yesterday's date. I need to replace it with the current date.
Here is what I've tried:
<target name="replace">
<tstamp >
<format property="touch.time" pattern="yyyyMMdd"/>
</tstamp>
<property name="Feature.dir" location="../feature" />
<!--Didnt Work-->
<copy file="${Feature.dir}\build.xml" tofile="${Feature.dir}\build1.xml"
filtering="yes" overwrite="yes">
<filterset>
<filter token="Version" value="1.0.0.${touch.time}"/>
</filterset>
</copy>
<!--Didnt work
<replacetoken><![CDATA[<property name="Version" value=""/>]]>
</replacetoken>
<replacevalue><![CDATA[<property name="Version"value="1.0.0.${touchtime}" />]]>
</replacevalue>
-->
<!-- Didnt work
<copy file="${Feature.dir}/build.xml" tofile="${Feature.dir}/build1.xml" >
<filterchain>
<tokenfilter>
<replaceregex pattern="^[ \t]*Version[ \t]*=.*$"
replace="Version=1.0.0.${touch.time}"/>
</tokenfilter>
</filterchain>
</copy>
-->
</target>
I would use replaceregex inside a filterchain.
For example:
<copy file="${Feature.dir}\build.xml" tofile="${Feature.dir}\build1.xml"
filtering="yes" overwrite="yes">
<filterchain>
<tokenfilter>
<replaceregex pattern="1.0.0.[0-9.]*" replace="1.0.0.${touch.time}"/>
</tokenfilter>
</filterchain>
</copy>
If you want to replace the file, feel free to copy to a temp file and move it back.
<tempfile property="build.temp.file.name"/>
<copy file="${Feature.dir}\build.xml" tofile="${build.temp.file.name}" ... />
<move file="${build.temp.file.name}" tofile="${Feature.dir}\build.xml" />

how can i filter whole line which contains a specific string

i have the following Ant script for reading revisionlog.txt file line by line and printing all the line.
<target name="line_by_line">
<loadfile property="file" srcfile="revisionlog.txt"/>
<for param="line" list="${file}" delimiter="${line.separator}">
<sequential>
<echo>#{line}</echo>
</sequential>
</for>
</target>
but here i want to print those line only which contains Comments: string.
how can i do this.
you may use loadfile combined with a filterchain, f.e. :
<loadfile property="yourline" srcfile="revisionlog.txt">
<filterchain>
<linecontains>
<contains value="Comments:"></contains>
</linecontains>
</filterchain>
</loadfile>
<echo>$${yourline} = ${line.separator}${yourline}</echo>
if you need more control, use <linecontainsregexp>
see Ant manual for FilterChains and FilterReaders

Resources