Exclude all files in a filelist - ant

I have a file list as given below which is used by an existing target.
I want to write a new target which zips all files from the directory files except files which is given in the filelist (properties).
<filelist id="excludesFiles" dir="${client.config.dir}" files="audit.properties,configuration.xml,portal.properties,services.xml,torque.properties,turbine.properties"/>

You should define your fileset as follows
<filelist id="zipfiles" dir="${client.config.dir}" excludes="audit.properties,configuration.xml,portal.properties,services.xml,torque.properties,turbine.properties"/>
<zip destfile="abc.zip"
<fileset refid="zipfiles"/>
</zip>
The fileset now includes all files in the config dir except the ones given in the excludes list.

Related

ant to exclude some files

I am trying to use ant to copy files from sourceDir to destDir, and I also don't want to copy some files, I keep the file names I want to exclude in file: excludelist.txt. How could I implements this is an ant target ?
Use copy task with nested fileset using nested exludesfile.
From ant manual FileSet :
excludesfile => the name of a file; each line of this file is taken to
be an exclude pattern.
snippet :
<target name="foo">
<copy todir="path/to/destDir">
<fileset dir="path/to/sourceDir">
<excludesfile name="excludelist.txt"/>
</fileset>
</copy>
</target>

Ant: Zip the contents of a directory, without the top-level directory itself

How can I use Ant to create zip containing all the files and nested directories inside some root directory, but exclude the top-level directory itself from the zip.
For example, say I have this file structure:
/foo
splat.js
/bar
wheee.css
I want the zip to contain splat, and wheee inside /bar, but I don't want all that to be contained inside a 'foo' directory. In other words, unzipping this into /honk should not create a foo directory; splat and bar should end up at the root of /honk.
I'm currently using this (extraneous details removed):
<zip destfile="${zipfile}" basedir="" includes="${distRoot}/**/*.*" />
What kind of fileset select can replace that 'includes' spec to achieve this?
It's not dynamic, but using the fullpath attribute allows you to define the path struture of the zip file. See ant documentation: http://ant.apache.org/manual/Types/zipfileset.html
<zip destfile="YourZipName.zip">
<zipfileset fullpath="splat.js" dir="foo" includes="splat.js"/>
<zipfileset fullpath="bar/wheee.css" dir="foo/bar" includes="wheee.css"/>
</zip>
This may get you want you want dynamically. It should pull in everything in the foo dir then zip it up. It's just a few extra steps.
<mkdir dir="DirToBeZipped"/>
<copy todir="DirToBeZipped">
<fileset dir="foo" includes="*"/>
</copy>
<zip destfile="YourZipName.zip" basedir="DirToBeZipped"/>

How to check if files in one directory exist in another directory?

I am looking for a means to have Ant check a target directory for the existence of files in a list/set defined by a source directory.
The Ant target only needs to determine if the files in a source directory also currently exist in the target directory. No copying/moving/deleting necessary.
Can this be done in Ant?
Take a look at the Ant resource collection set operators. For example:
<fileset id="source" dir="source_dir" />
<fileset id="target" dir="target_dir" />
<difference id="difference">
<resources refid="source"/>
<resources refid="target"/>
</difference>
<echo message="${toString:difference}" />
You can use a dirset if you're only interested in directories.

Ant -- copying files and subdirectories from only one subdirectory on a tree

I'd like to copy files and subdirectories using Ant from a single subdirectory without copying the rest of the directory structure and contents. For example, I'd like to copy dir_3, its files (file_1 to file_n) and subdirectories (dir_4 and dir_5), but not dir_1 nor dir_2. Is there a pattern that I can use to do this?
temp
\--dir_1
\--dir_2
|
\--dir_3
|
\--dir_4
\--dir_5
\-- file_1
|
\--file_n
Thanks.
<copy todir="${copy.dir}">
<fileset dir="temp">
<include name="**/dir3/**"/>
</fileset>
</copy>
When you use the include directive, it will only include the files that match the pattern you give it. In this case, I'm copying only those files that have /dir3/ somewhere in their full path name. This includes sub-directories under dir3 and all files under dir3.
You can use the exclude directive to override the include directives:
<copy todir="${copy.dir}">
<fileset dir="temp">
<include name="**/dir3/**"/>
<exclude name="**/dir3/*"/>
</fileset>
</copy>
This will copy all sub-directories and files in those sub directories, but not the files under dir3 itself. The * matches all files in the directory while ** matches the all the files in the entire directory tree.
Notice this will create a directory temp/dir2/dir3. If I want temp/dir3, I have to set my fileset to the parent directory of dir3:
<copy todir="${copy.dir}">
<fileset dir="temp/dir2">
<include name="dir3/**"/>
</fileset>
</copy>
Doing this:
<copy todir="${copy.dir}">
<fileset dir="temp/dir2/dir3"/>
</copy>
Will create a directory temp with all the files directly under dir3 directly under temp. There will also be a temp/dir4 and temp/dir5 directory containing all the files (and directory trees) under those directories.
<copy todir="/some/path/foobar" verbose="true">
<fileset dir="/some/path/temp/dir2" includes="**"/>
</copy>
just use a fileset starting from dir2 including all dirs and files below..
verbose = true to echo all the files copied
May be you need to use overwrite = true also if the dir that is specified by todir
attribute already exists, otherwise existing files won't be overwritten by copy task

Apache Ant: Selecting files with fileset?

It's really easy to select a file with a specific filename, or filetype using fileset in ANT, however I have yet not figured out how to write a fileset that remove all files with a filename beginning with a dot, such as .builtpath, and .hgignore, but excluding .htaccess;
Here's my current file:
<delete includeemptydirs="true">
<fileset dir="${temp.dir}/fromRepo">
<exclude name=".htaccess"/>
<include name="**/*" /> <!-- How to select files starting with .?!-->
</fileset>
</delete>
Suggest you try:
<delete includeemptydirs="true">
<fileset dir="${temp.dir}/fromRepo">
<exclude name="**/.htaccess"/>
</fileset>
</delete>
If you don't specify any wildcard - as in ".htaccess" then that rule will only match the exact file name, i.e., '.htaccess' in the top-level directory of the fileset. Prepending the directory wildcard ** to .htaccess will tell Ant to exclude from the delete all files called '.htaccess' found under the directory hierarchy of the fileset.
There's an implicit include of all files if you don't specify any include rule - so no need to specify the 'global' include.
One thing to watch out for - setting includeemptydirs true will remove any empty directories when using a fileset with the delete task. A directory will only be considered empty if it doesn't contain any files. In other words: directories containing a file called '.htaccess' will not be deleted, but those with a '.htaccess' file will not be deleted - hope that's what you need.

Resources