Apache Ant: Selecting files with fileset? - ant

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.

Related

adding the specified folders in the jar created using ant

I have a folder structures
build/classes/com/vaannila/domain/.class files
build/classes/com/vaannila/service/.class files
build/classes/com/vaannila/web/.class files
My requirement is to create a jar file using ant, how can I include the specified folders in the jar,say I want include only domain and service (or) domain and web in the jar, like the way I create the jar using Eclipse(Export-->archive file).
Can anyone help me with this please?
You can use include and exclude tags in the fileset element, and choose specific files...
<jar destfile="${jar.dir}/${jar.file.name}" index="true"
manifest="${manif.dir}/${manif.file.name}">
<fileset dir="${build.classes.dir}" >
<include/>
<exclude/>
</fileset>
</jar>
<fileset dir="${build.classes.dir}">
<exclude name="**/property/*"/>
<include name="**/abcd/*"/>
</fileset>
This way you can remove/include respective directories.

Ant: renaming a group of files subject to a condition

I'm using Ant 1.8.2. Given a directory, how do I add an ".html" extension to all files in the directory and its sub-directories only if those files don't already have an .html extension?
There's an example in the Ant move task docs for renaming .bak files that is similar, here it is adjusted for .html files under a directory called my_dir:
<move todir="my_dir" includeemptydirs="false">
<fileset dir="my_dir">
<exclude name="**/*.html" />
</fileset>
<mapper type="glob" from="*" to="*.html" />
</move>
The fileset excludes files that already have the target extension, the mapper defines the renaming pattern.

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

Delete all files in a directory w/o subdirectories with Apache Ant

I need an Apache Ant target that deletes all files in a directory but does not touch subdirectories.
In my current approach I have to explicitly name the subdirectories I want to skip (atm just "src/").
<delete>
<fileset dir="${dist.dir}" excludes="src/" />
</delete>
But I don't like it. That way I would have to modify the target everytime something changes in the subdirectory structure.
Any ideas?
This should work:
<delete>
<fileset dir="${dist.dir}">
<include name="*"/>
</fileset>
</delete>
The * wildcard should only delete the files at the top level, not directories or subdirectories. If you wanted it to be recursive, you'd need to use **/* instead.

Copy content of subfolders with Ant

How can I copy content of all subfolders of given folder using Ant?
i.e. I have such folder structure
folder/
folder/sub1/1.txt
folder/sub1/f1/1.txt
folder/sub2/2.txt
...
I don't know exact names of subfolders. And I need to copy content from all of them into one folder (keeping the structure of content, i.e. copying all files into one dir using flatten isn't a solution). I need to get
newfolder/1.txt
newfolder/1/1.txt
newfolder/2.txt
...
Does fileset allows to group subfolders in such a way?
** stands for zero or more directories, and usage of * as directory name is disallowed, i.e. <fileset dir="${dir}/*/" /> isn't acceptable.
Thanks in advance, Yury
<copy toDir="newfolder">
<fileset dir="folder">
<include name="*/**"/>
<exclude name="*"/>
</fileset>
<regexpmapper from="^[^/]*/(.*)$$" to="\1" handledirsep="true"/>
</copy>
You only need to specify handledirsep if you ever intend to run this script in Windows.

Resources