listing all files and subdirectories using ant - 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.

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>

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>

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.

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