Delete all files and folders via Ant FTP task - ant

How to delete all files and folders inside specified remote folder in Ant?
I've tried the following:
<ftp server="${ftp.host}" userid="${ftp.user}"
password="${ftp.pass}" remotedir="${ftp.remotedir}" action="del">
<fileset>
<include name="**/*"/>
</fileset>
</ftp>
it deletes all files, but not folders.
(if I write here <include name="*.txt"> instead it works as expected - deletes all txt files, but what if I want to delete all files and folders?)

You should use another command: rmdir.
This command does not remove folder specified in the remotedir parameter.
The sample based on information from ant.apache.org:
<ftp action="rmdir"
server="${ftp.host}"
userid="${ftp.user}"
password="${ftp.pass}"
remotedir="${ftp.parentdir_for_remotedir}" >
<fileset>
<include name="${ftp.remotedir}/**"/>
</fileset>
</ftp>
The quote from site:
The directory specified in the remotedir parameter is never selected
for remove, so if you need to remove it, specify its parent in
remotedir parameter and include it in the pattern, like
"somedir/**".
Also worth noting is that rmdir will fail if there are anything but empty folders in the fileset specified.
From the same site:
As an example suppose you want to delete everything contained into
/somedir, so invoke first the task with action="delete", then
with action="rmdir" ...

Related

How to exclude empty directories from zipfileset

I have an ant target for creating zip like this -
<zip destfile="${dist}/myzip.zip">
<zipfileset prefix="product/install" includes="docs/resources/**,docs/*.*" excludes="docs/build.bat,docs/*.xml,docs/resources/*.html"/>
</zip>
Now, how do I ensure that empty directories don't get included in this zipfileset.
Eg: docs/resources directory only has html files, all of which I have excluded above. How do I make sure docs/resources folder doesn't get included.
Should I be checking for this manually everytime? or is there an option like includeEmptyDirs="false"?
I think there isn't an option for this in zip task, see documentation.
But what you can do is to make a copy with excludes/includes, and define to exclude the empty directories and then call the zip task on the copied folder:
<copy todir="tmp2" includeEmptyDirs="false">
<fileset dir="tmp1" excludes="**/*.txt"/>
</copy>
<zip>...
Documentation of copy

Ant Task to FTP only specified files

I have an Ant task that FTPs all files in a specified directory, and it uses a fileset:
<fileset dir="${publicDirectory}">
<include name="media/**/*" />
</fileset>
I have a file that contains all the files that I would like to include:
media/some/dir/1.txt
media/some/other/2.txt
...
How can I have the fileset read the file and only include whatever I've listed there?
I've tried quite a few things, but nothing seems to be able to get around a basic issue: The <ftp> task works only on filesets and not other types of resources. I've tried various filterchains, but to no avail.
The best I could come up with was using the Ant-Contrib <for> or <foreach> task to loop through the file and then use an <exec> task to execute the command line version of ftp.

How to create temporary directory in ant?

I'd like to create a temporary directory in ant (version 1.6.5) and assign it to a property.
The command "mktemp -d" would be ideal for this, but I cannot find similar functionality from inside ant
I can't find any official function in the docs apart from the tempfile task which apparently only creates files, not directories.
I'm considering using exec to call tempfile and get the result, however this will make my build.xml dependent on UNIX/linux, which I'd like to avoid.
Background: I'm trying to speed up an existing build process which builds inside networked filesystem. The build already copies all the source to a temporary directory, however this is on the same filesystem. I've tested changing this to /tmp/foo and it gives a worthwhile speed increase: 3mins vs 4mins.
You could combine the tempfile task with the java.io.tmpdir system property to get a file path to use to create a temporary dir:
<project default="test">
<target name="test">
<echo>${java.io.tmpdir}</echo>
<tempfile property="temp.file" destDir="${java.io.tmpdir}" prefix="build"/>
<echo>${temp.file}</echo>
</target>
</project>
Note that the tempfile task does not create the file (unless you ask it to). It just sets a property which you can use to create a file or dir.
This task sets a property to the name of a temporary file. Unlike
java.io.File.createTempFile, this task does not actually create the
temporary file, but it does guarantee that the file did not exist when
the task was executed.
Output in my environment:
test:
[echo] C:\Users\sudocode\AppData\Local\Temp\
[echo] C:\Users\sudocode\AppData\Local\Temp\build1749402932
The answer above only hints at how to create a temporary directory. The point is that merely returns a string. A more complete answer is
<target name="temptest" description="test making tempdir">
<tempfile property="mytempdir" destdir="${java.io.tmpdir}"/>
<tempfile property="mytempfile" destdir="${mytempdir}"/>
<tstamp>
<format property="now" pattern="MMMM dd yyyy"/>
</tstamp>
<copy tofile="${mytempfile}">
<string value="today=${now}"/>
</copy>
<property file="${mytempfile}"/>
<echo message="It it now ${today}"/>
</target>

How to iterate over Directories in Ant

I've a root directory and it contains many directories,in turn each sub-directory contains many directories and so on.For example if "A" is a root directory it contains sub-directories "A.a","A.b",so on... and each directory("A.a","A.b",etc) contains many directories.I want to copy the inner directories of "A.a" , "A.b" ,etc.. to other directory structure similar to the "A".Instead of copying the each directory I want to use loop that iterates every directory and it's sub-directories(even files).How to do that...Please help me out as I'm new to Ant...
For copying, use 'copy' and 'fileset':
<copy todir="./destination/dir">
<fileset dir="./source/dir">
<include name="**/*" />
</fileset>
</copy>
The include directive inside the fileset will cause Ant to review each directory recursively.
Other tasks that involve files and directories (such as move for ftp) will also accept filesets.

Ant delete task

I have several files with name abc* and i want to delete all those files. is it possible using ant task. For eg. my directory structure is:
c:\
myapp\
abc.xml
abc.txt
abc-1.2.xml
abc-abc.xml
abcdef.xml
pqr.xml
xyz.xml
abc\
so from this, i need to delete all abc* files. So if i use ant it should delete following:
abc.xml
abc.txt
abc-1.2.xml
abc-abc.xml
abcdef.xml
it should leave directory with abc*
Can somebody help me.
Almas
<target name="testingdelete" >
<delete>
<fileset dir="." includes="**/abc*"/>
</delete>
</target>
should work.
It deletes all files with abc* and leaves behind directories named abc. It will delete from all sub-directories as well.

Resources