How to exclude empty directories from zipfileset - ant

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

Related

Ant - Match all files in a directory

I'm trying to copy all the files in a directory using Ant Fileset pattern.
I tried the following,
**/* and **/*.* but neither of them copies entire directory structure.
Is there any thing wrong in my file set pattern?
If you are using fileset with file attribute, it is just for one single file.
To copy all the contents of a directory to another, you should use dir attribute:
<copy todir="${destination_dir}">
<fileset dir="${source_dir}"/>
</copy>

Delete all files and folders via Ant FTP task

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" ...

ant zip; exclude all sub-directories and files

When creating a zip from ant, how can I exclude all sub directories and files from a given directory?
I have tried the following but it doesn't seem to prevent them from being included in the zip
<target name="zip">
<zip destfile="C:\Projects\example\builds\.zip"
excludes="C:\Projects\example\logs\**\*.*">
...
...
</zip>
</target>
From reading the documentation, and from reading the ant definitive guide I would assume that **\ should exclude any directory, and *.* would exclude any file of any extension
I want to include the logs directory, but nothing inside it.
I would recommend the following:
Change the name of your destfile to "C:\Projects\example\builds\logs.zip"
Set your basedir to "C:\Projects\example\"
Change your excludes value to "C:\Projects\example\logs\**\*" (that means any file)
Another option might be to use the project-defined basedir, and change all your paths to relative UNIX-like values.

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