File delete is not working via ant script - ant

I have written an Ant script in which there is some block of codes which create a temp file and apply ant filters to that temp file and generate new one.
following is the code:
<copy tofile="${file.report.name}.html" file="${file.report.name}-temp.html">
<filterchain>
<tokenfilter>
<replaceregex pattern="\[(echo|script|apply|copy)\]" replace="" />
</tokenfilter>
</filterchain>
<filterchain>
<linecontains negate="true">
<contains value="Copying"/>
</linecontains>
</filterchain>
</copy>
<antcall target="final.cleanup"/>
and then i call a target for deletion of generated temp file. but its giving me some error saying that script is unable to delete the respected file.
Following code block i am using for deletion of temp file:
<target name="final.cleanup">
<delete file="reports/report-temp.html"/>
</target>
What went wrong? Is this file is being used in some process that's why it giving me the error !

Related

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 script throwing an exception while calculating md5

HI I am using ant script to calcalte md5 of two files in a particular folder.This the is the script which i have written
<?xml version="1.0"?>
<project name="Hello World Project" basedir="." default="info">
<property name="cms.dir" value="D:\CMS\webclient\components\CMS\Address\AddressSearch" />
<taskdef resource="net/sf/antcontrib/antlib.xml"/>
<target name="info">
<echo>Hello World - Welcome to Apache Ant!</echo>
<fileset id="src.files" dir="${cms.dir}" casesensitive="yes">
<include name="**/*.uim"/>
<include name="**/*.properties"/>
</fileset>
<pathconvert pathsep="${line.separator}" property="sounds" refid="src.files">
</pathconvert>
<echo file="sounds.txt">${sounds}</echo>
<loadfile property="files" srcFile="./sounds.txt"/>
<for list="${files}" delimiter="," param="file1">
<sequential>
<echo>#{file1}</echo>
<checksum file="#{file1}" todir="./checksum" />
</sequential>
</for>
</target>
</project>
The file name is getting printed correctly but when i am using the same file to calculate the md5 it is throwing an exception like this
BUILD FAILED
C:\build.xml:15: The following error occurred while executing this line:
C:\build.xml:18: Could not find file D:\CMS\webclient\components\CMS\Address\Add
ressSearch\CMS_addressSearchPopUp.properties
D:\CMS\webclient\components\CMS\Address\AddressSearch\CMS_addressSearchPopUp.uim
to generate checksum for.
any help regarding this
You are using new line when creating "sounds", but is using comma to split.
<pathconvert pathsep="${line.separator}" property="sounds" refid="src.files">
Then this is written to a file - sounds.txt
Then reads the file, to split using comma. (","
<for list="${files}" delimiter="," param="file1">
If I understood the question correctly, you should split using new line
<for list="${files}" delimiter="${line.separator}" param="file1">

ANT replacing strings in specified files using file with properties

I have a properties in file dev.properties and they look like this:
test.url=https://www.example.com/
[...]
and in project files there is a token [[test.url]] which I want to replace by https://www.example.com/. I just want to define all tokens in dev.properties and use them in build script, but without modifying build script and I want to replace those tokens in a specified files like *.php, *.html, etc.
Can someone give me a suggestions how to do it? Thanks.
try this:
<copy file="input.txt" tofile="output.txt">
<filterchain>
<replaceregex pattern="\$\{" replace="{" />
<filterreader classname="org.apache.tools.ant.filters.ReplaceTokens">
<param type="propertiesfile" value="properties.txt"/>
<param type="tokenchar" name="begintoken" value="{"/>
<param type="tokenchar" name="endtoken" value="}"/>
</filterreader>
</filterchain>
</copy>
Founded here: Ant replace token from properties file
In the following Ant script, replace the src-root property with the root directory containing the tokenized files:
<project name="ant-replace-tokens-with-copy-task" default="run">
<target name="run">
<!-- The <copy> task cannot "self-copy" files. So, for each -->
<!-- matched file we'll have <copy> read the file, replace the -->
<!-- tokens, and write the result to a temporary file. Then, we'll -->
<!-- use the <move> task to replace the original files with the -->
<!-- modified files. -->
<property name="src-root" location="src"/>
<property name="filtered-file.extension" value="*.filtered-file"/>
<copy todir="${src-root}">
<fileset dir="${src-root}">
<include name="**/*.html"/>
<include name="**/*.php"/>
</fileset>
<globmapper from="*" to="${filtered-file.extension}"/>
<filterchain>
<filterreader classname="org.apache.tools.ant.filters.ReplaceTokens">
<param type="propertiesfile" value="dev.properties"/>
</filterreader>
</filterchain>
</copy>
<move todir="${src-root}">
<fileset dir="${src-root}" includes="**"/>
<globmapper from="${filtered-file.extension}" to="*"/>
</move>
</target>
</project>
You specified that you do not want to edit your build script so this answer does not qualify but may still be useful to other readers.
If you were willing to edit your target file to use the format ${test.url} instead of [[test.url]] then ExpandProperites would be an excellent choice.

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

Filtering Files In-Place with Ant?

I have a directory of files for which I'd like to do "in-place" string filtering using Apache Ant (version 1.7.1 on Linux).
For example, suppose that in directory mydir I have files foo, bar, and baz. Further suppose that all occurences of the regular expression OLD([0-9]) should be changed to NEW\1, e.g. OLD2 → NEW2. (Note that the replace Ant task won't work because it does not support regular expression filtering.)
This test situation can be created with the following Bash commands (ant will be run in the current directory, i.e. mydir's parent directory):
mkdir mydir
for FILE in foo bar baz ; do echo "A OLD1 B OLD2 C OLD3" > mydir/${FILE} ; done
Here is my first attempt to do the filtering with Ant:
<?xml version="1.0"?>
<project name="filter" default="filter">
<target name="filter">
<move todir="mydir">
<fileset dir="mydir"/>
<filterchain>
<tokenfilter>
<replaceregex pattern="OLD([0-9])" replace="NEW\1" flags="g"/>
</tokenfilter>
</filterchain>
</move>
</target>
</project>
Running this first Ant script has no effect on the files in mydir. The overwrite parameter is true by default with the move Ant task. I even fiddled with the granularity setting, but that didn't help.
Here's my second attempt, which "works," but is slightly annoying because of temporary file creation. This version filters the content properly by moving the content to files with a filtered suffix, then the filtered content is "moved back" with original filenames:
<?xml version="1.0"?>
<project name="filter" default="filter">
<target name="filter">
<move todir="mydir">
<globmapper from="*" to="*.filtered"/>
<fileset dir="mydir"/>
<filterchain>
<tokenfilter>
<replaceregex pattern="OLD([0-9])" replace="NEW\1" flags="g"/>
</tokenfilter>
</filterchain>
</move>
<move todir="mydir">
<globmapper from="*.filtered" to="*"/>
<fileset dir="mydir"/>
</move>
</target>
</project>
Can the first attempt (without temporary files) be made to work?
See the replace task:
<replace
dir="mydir"
includes="foo, bar, baz">
<replacefilter token="OLD" value="NEW" />
</replace>
or the replaceregexp task:
<replaceregexp
file="${src}/build.properties"
match="OldProperty=(.*)"
replace="NewProperty=\1"
byline="true"/>

Resources