User passes a list of files in an XML file, below will be the sample:
<property-bundle name = "abc">
<action>clean</action>
<target-location>/vst/property/pog/</target-location>
<file-name>test1.props</file-name>
<file-name>test2.props</file-name>
<file-name>test3.props</file-name>
</property-bundle>
Now based on that action remove, I have to incorporate logic in build.xml to delete the files in the directory , but for that I want to perform a validation only if the file exists then remove or else throw the build failure error. I was able to read the values from the user input XML and takes those files into a file list property
<property name="file.list" value="test1.props,test2.props,test3.props"/>
<target name = "clean">
<delete>
<fileset dir="${target.location}" includes = "${file.list}"/>
</delete>
</target>
but with the clean target it only validates if the directory exists since it is fileset but does not do the validation if file exists , I read that filelist does validation for file exists but filelist can work with delete.
Since we are using Ant 1.6.5 in our environment I can not use antcontrib , It takes whole lot of process and approvals to upgrade Ant now , Can you please guide me on how it can be achieved with the pure Ant.
You should be able to just use
delete
's@failonerror
attribute which throws an error if the file cannot be deleted.The above will delete files and then error when it doesn't find a file, leaving you in a partially deleted state. If you want to avoid partial deletions, you can run another task to check first
by attempting to copy to a temporary directory, failing if some of the target files did not exist.
Folks,
Thank you all for your outstanding help & contributions , I have finally achieved this with below
Thanks again for all of your valuable time and guidance.
This works for me.. hope this helps..
Ant is not a programming language and therefore has no native looping mechanism. In the absence of external plugins a trick that can used is an XSL transformation. Process the input XML file into a Ant script which implements the desired operation on each file.
Example
Run the build and the files listed in "files.xml" are deleted:
Run the build a second time and an error is generated:
build.xml
Use the xslt task to generate a temporary Ant script containing the desired file deletion logic:
files-process.xsl
The following stylesheet generates an Ant script:
build-tmp.xml
To aid readability I have formatted the generated Ant script:
Note:
Software used
This example was tested with the following software versions:
It is possible to loop over files with Ant-Contrib Tasks and then it will look something like this: