ant zip; exclude all sub-directories and files - ant

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.

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>

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

How to delete a symlink using Ant?

I am trying to delete a symlink in ant script using the below line :
<symlink action="delete" link="/path/of/link/symlink"/>
But it is showing an error:
Could not create tempfile in /directory/where/symlink/points
The /directory/where/symlink/points is supposed to be read-only.
Is there a way in which I could just delete the symlink and not mess up other things? It's a part of a big script.
Symlinks pointing to read-only resources may be deleted using the <delete> Ant task.
<target name="delete-symlink">
<delete file="/path/of/link/symlink" followsymlinks="false"
removenotfollowedsymlinks="true" />
</target>
And this is from the delete Ant task documentation:
removeNotFollowedSymlinks Whether symbolic links (not the files/directories
they link to) should be removed if they haven't been
followed because followSymlinks was false or the
maximum number of symbolic links was too big. Since
Ant 1.8.0
Hope it works.

listing all files and subdirectories using ant

I am trying to create a rpm package using ant task for that I need to create specfile which will have all the file names in the following format
%attr(0755, root, root) %dir dir1
%attr(0755, root, root) %dir dir1/dir2
%attr(0755, root, root) %dir dir1/dir2/dir3
%attr(0500, root, root) dir1/file1
%attr(0500, root, root) dir1/dir2/file1
I have such directory structure created during my build process but using ant I am not able to list all the files and directories which I can then write into my specfile
following is what I have tried to list the files but it does not differentiate between files and directory , moreover I need some way to iterate over the list.
<fileset id="dist.contents" dir="${nativePackageDir}" includes="**"/> |
<property name="prop.dist.contents" refid="dist.contents"/> | <target name="javaobject-library" depends="props">
<echo>${prop.dist.contents}</echo>
<dirset id="dist.contents" dir="${nativePackageDir}" includes="*"/>
<property name="prop.dist.contents" refid="dist.contents"/>
<echo>${prop.dist.contents}</echo>
Using dirset instead of fileset should fix your problem.
You simply have to write in java an ant task implementation, to which you'll provide as parameters the input directory and the path of the specfile you want to be written.
I find it better and more manageable to have reusable ant tasks in java, instead of having gigantic ant xml files.

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