Ant Copying Empty Directories - ant

I am still very new to ant and, although I know coldfusion, I don't know very much about java conventions, but I know that ant is built using java conventions. That being said I am working on an ant process to copy a project to a temp folder, change some code in the project, and then push the temp directory up to an FTP. I am trying to exclude all of my git, eclipse, and ant files from the copy so that my testing platform doesn't get cluttered. I setup a target to do the copy, but it seems that Ant not only is ignoring my excludes (which I am sure I wrote wrong), but it is only copying top level directories and files. No recursive copy. My current target is:
<target name="moveToTemp" depends="init">
<delete dir="./.ant/temp" />
<mkdir dir="./.ant/temp" />
<copy todir="./.ant/temp">
<fileset dir=".">
<include name="*" />
<exclude name=".*/**" />
<exclude name=".*" />
<exclude name="build.xml" />
<exclude name="settings.xml" />
<exclude name="WEB-INF/**" />
</fileset>
<filterset>
<filter token="set(environment='design')" value="set(environment='testing')" />
</filterset>
</copy>
</target>
I know that I am not doing my excludes right, but I don't know what I am doing wrong with them. I see double asterisks (**) used all the time in Ant but I can't figure out

By default an Ant fileset will (recursively) include all files under the specified directory, equivalent to:
<include name="**/*" />
That's the implicit include. If you supply an include, it overrides the implicit one.
Your include
<include name="*" />
Says 'match any file in the fileset directory', but that excludes traversal of subdirectories, hence your issue. Only files and the top-level directories are being copied.
See Patterns in the Ant docs for directory-based tasks: ** matches any directory tree (zero or more directories).
For your case you should be able to simply remove the 'include', so that the implicit 'include all' applies.
Suggest you also investigate the defaultexcludes task, which lets you set up this sort of thing once for the whole project.

Responding to the title of the question. You can include copy of empty directories as follows. (includeemptydirs attribute)
Example:
<copy includeemptydirs="true" todir="${directory}${file.separator}sentinel_files">
<fileset dir="${basedir}${file.separator}sentinel_files"/>
</copy>
Use the documentation provided in:
https://ant.apache.org/manual/Tasks/copy.html

Related

Using Ant to delete a file based on existence of another file

I need to write an ant task to selectively delete files.
In a directory, I have the following jars:
acme.jar
acme-201105251330.jar
I want to delete acme.jar because acme-*.jar exists.
Here's what I've tried:
<target name="-check-use-file">
<available property="file.exists">
<filepath> <fileset dir=".">
<include name="./test-*.jar"/> </fileset>
</filepath>
</available>
</target>
<target name="use-file" depends="-check-use-file" if="file.exists">
<!-- do something requiring that file... -->
</target>
Thanks
Have a look at If/Unless Attributes, examples given there seem to be exactly what you are looking for.

Relocate contents of dynamic folder

I have a zip file which has one base folder inside it with other content inside that. I don't always know what that base folder is going to be called until I unzip it.
I'd like to move that base folder, and rename it at the same time, in ant - but can't seem to find out how. I've written code to extract the contents of the zip file to ${local.sdk.dir}/temp/ but from here i can't work out how to rename/move the extracted folder
<move todir="${local.sdk.dir}/${remote.sdk.file.name}">
<fileset dir="${local.sdk.dir}/temp/<WHAT_DO_I_PUT_HERE?>"></fileset>
</move>
also tried
<move todir="${local.sdk.dir}/${remote.sdk.file.name}" includeEmptyDirs="yes" verbose="true">
<fileset dir="${local.sdk.dir}/temp/" >
<include name="**/*" />
</fileset>
</move>
and played about with this, but closest I can get without ant throwing an error is to copy the contents of the temp dir, not the base folder within temp.
You can do all this in one step - copy from the zip file and rename the files changing the dir name as you copy. The copy task accepts a nested resource collection, so you can use a zipfileset to specify the files to copy directly from the zip file.
In order to rename the files as they are copied, you can use a mapper, which the copy task also takes as a nested element. In this case, a cutsdirmapper looks like the tool for the job.
So, if I've understood what you want to do correctly, something like this should work:
<copy todir="${local.sdk.dir}/${remote.sdk.file.name}">
<zipfileset src="${your.zip.file}" />
<cutdirsmapper dirs="1" />
</copy>
cutdirsmapper is only available in Ant 1.8.2 onward, so if you're using an earlier version, you could try a regexpmapper:
<regexpmapper from="[^/]*(.*)" to="\1" />
Similar to this question
<target name="relocate_sdk_folder">
<path id="sdk_folder_name">
<dirset dir="${local.sdk.dir}/temp/">
<include name="*"/>
</dirset>
</path>
<property name="sdk_folder_name" refid="sdk_folder_name" />
<echo message="renaming ${sdk_folder_name} to ${remote.sdk.file.name}" />
<move file="${sdk_folder_name}" tofile="${local.sdk.dir}/${remote.sdk.file.name}" />
</target>

copying multiple directories (and contents) in one shot

I've been using ant for nearly a decade, but every so often I need to do something beyond my-ordinary experience. This one lacked an obvious answer (and the intuitive approaches led to dead ends)
Problem:
Copy several subdirectories (and their contents) in directory "example" to new directory "myInstance". To clarify, copy some, but not all subdirectories in the source directory.
Source directory:
example/
ignoreThisDirectory/
ignoreThisOneAlso/
lib
etc/
webapps/
Attempt: Dead End
This attempt at first appeared to work. It created the subdirectories lib, etc,webapps. However 'copy' did not copy their contents; i was left with empty subdirectories.
<copy todir="myInstance" >
<dirset dir="example" includes="lib etc webapps"/>
</copy>
Successful But Verbose
In the end, I had to copy each directory individually, which seem verbose and non-DRY:
<copy todir="myInstance/etc">
<fileset dir="example/etc"/>
</copy>
<copy todir="myInstance/lib">
<fileset dir="example/lib" />
</copy>
<copy todir="myInstance/webapps">
<fileset dir="example/webapps" />
</copy>
thanks in advance
You can specify multiple inclusion and exclusion rules in a fileset. If you don't specify an inclusion rule, the default is everything is included, except anything that is excluded at least once by an exclude rule.
Here's an inclusive example:
<property name="src.dir" value="example" />
<property name="dest.dir" value="myInstance" />
<copy todir="${dest.dir}">
<fileset dir="${src.dir}">
<include name="lib/**" />
<include name="etc/**" />
<include name="webapps/**" />
</fileset>
</copy>
Note the ** wildcard that will bring in the full directory tree under each of the three 'leading-edge' sub-directories specified. Alternatively, if you want to specifically exclude a few directories, but copy over all others, you might omit inclusion (and thereby get the default all-inclusive behaviour) and supply a list of exclusions:
<copy todir="${dest.dir}">
<fileset dir="${src.dir}">
<exclude name="ignoreThisDir*/" />
<exclude name="ignoreThisOne*/" />
</fileset>
</copy>
You could further boil the particular example you gave down to one exclusion pattern:
<exclude name="ignore*/" />

ANT How to delete ONLY empty directories recursively

Does anyone know how to recursively delete "empty" directories with ANT (empty includes directories that only contain ".svn" etc).
I know ant allows you to "includeEmptyDirs=true" but I want it to ONLY delete a directory if it is empty (and actually I'd probably need to walk up the recursive chain and delete the directory it was contained in if it is now empty).
Basically as part of our build process we copy over a set of directories that contain a bunch of other nested directories containing various XML and data, and when we move the location for that data our "copy" and checkin build process doesn't really work, and because we are checking into another source control (SVN), wiping out the directories and copying over isn't really an option either (we'd be blowing away the ".svn" folders).
Before we copy over the new builds I can "clear" out the directories by doing the following:
<delete>
<fileset dir="${webplatformBin}" includes="**/*"/>
</delete>
This leaves every directory (with the ".svn") as an empty directory and then I copy over the new files. After they're copied I'm not sure how I can clear out the empty directories that are left (if we've completely moved where the top-level data directory is etc.).
For example if I had a /projectA/data/localization/text.xml file and I moved it to /projectB/data/localization/text.xml, I would end up with an empty folder /projectA/data/localization/ (that would only contain a .svn folder).
Here's the best answer I've been able to come up with:
<delete includeemptydirs="true">
<fileset dir="${dirToStartFrom}" >
<and>
<size value="0"/>
<type type="dir"/>
</and>
</fileset>
</delete>
I then wrapped it in a macro so I can pass the dir name in from any target:
<!-- Find and delete empty folders under dir -->
<macrodef name="deleteEmptyFolders">
<attribute name="dir"/>
<sequential>
<delete includeemptydirs="true">
<fileset dir="#{dir}" >
<and>
<size value="0"/>
<type type="dir"/>
</and>
</fileset>
</delete>
</sequential>
</macrodef>
Like so:
<target name="clean">
<deleteEmptyFolders dir="build"/>
<deleteEmptyFolders dir="common"/>
<deleteEmptyFolders dir="lib"/>
</target>
Here's what I cooked up:
<!-- next three targets are connected
To remove empty folders from XXX folder. Process is recursed 3 times. This
is necessary because parent directories are not removed until all their children
are (if they are empty), and parents are processed before children
My example will process structures 3 deep, if you need to go deeper
then add members to the list like list="1,2,3,x,x,x,x,x,x" -->
<target name="rmmtdirs">
<foreach list="1,2,3" target="rmmtdirs_recurse" param="num"/>
</target>
<target name="rmmtdirs_recurse">
<foreach target="rmmtdir" param="rmdir">
<path>
<dirset dir="${XXX}"/>
</path>
</foreach>
</target>
<target name="rmmtdir">
<echo message=" Removing: ${rmdir} "/>
<delete includeemptydirs="true">
<fileset dir="${rmdir}" excludes="**/*"/>
</delete>
</target>
I was able to delete all the empty directories starting with the current working directory with the following:
<delete includeemptydirs="true">
<fileset dir="." includes="**" excludes="**/*.*" />
</delete>
If it is not sufficient to completely clear the target location (use defaultExcludes="false" to ensure the .svn folders are deleted), you could try writing a custom ant task to traverse the file system below the target, deleting empty directories as you move back up from each leaf.
This is probably easier to do with a batch file that gets called from ant.
You can use Raymond Chen's script, but it doesn't work if there are spaces in the names.

How do I build an Ant TarTask for this tar command?

tar zcvf Test.tar.gz /var/trac/test /var/svn/test
So far I have:
<target name="archive" depends="init">
<tar compression="gzip" destfile="test.tar.gz">
<tarfileset dir="/">
<include name="/var/trac/test" />
</tarfileset>
<tarfileset dir="/">
<include name="/var/trac/svn" />
</tarfileset>
</tar>
</target>
With debugging on, it always says "No sources found" so I'm a bit perplexed on what to do next.
There are several things here that can go wrong:
Are /var/trac/test and /var/svn/test files or directories? Real tar would work fine with both; your task - the way it's written right now - would only work with files but not folders.
Do you (or, rather, does Ant process) have adequate permissions?
<include> elements contain patterns, which are generally relative to base dir. You're specifying them as absolute.
I would rewrite the above as:
<target name="archive" depends="init">
<tar compression="gzip" destfile="test.tar.gz">
<tarfileset dir="/var/trac" prefix="/var/trac">
<include name="test/**" />
<include name="svn/**" />
</tarfileset>
</tar>
</target>
prefix allows you to explicitly specify the path with which files in Tar archive should begin.
/** within <include> tells Ant to take all the files from that folder and all its subfolders. If /var/trac/test and /var/svn/test are indeed files and not folders then you can omit that.

Resources