How to do recursive copy of files and directories using ant task - ant

Ant has inbuilt Copy task to copy multiple files.
I tried to define following target in build.xml file
<target name="copyFile">
<copy todir="../CHECK">
<fileset dir=".">
<patternset id="AllFiles">
<include name="*"/>
</patternset>
</fileset>
</copy>
</target>
It is copying files and directories. However content within directories is not copied, instead directories are copied as empty to destination "../CHECK". Does Ant copy task provides capability to do recursive copy of files and directories

I found the answer
name pattern in include should be "**" instead of "*". It does recursive copy of all contents
<target name="copyFile">
<copy todir="../CHECK">
<fileset dir=".">
<patternset id="AllFiles">
<include name="**"/>
</patternset>
</fileset>
</copy>
</target>

Related

How to copy a files to a particular dir using ant?

i need to copy test.bat into the bin folder
i worte a code in build.xml such that it should copy a file from bin/test.bat to dist/intall/windows/
but it not copying
project structure
project
|-dist-install-windows-bin
|-etc-bin-test.bat
|-src-build.xml
<target name="copy">
<copy todir="./dist/install/windows/bin">
<fileset dir=".">
<include name="etc/bin/test.bat"/>
</fileset>
</copy>
</target>
Give parent directory as "dir" attribute
<fileset dir="..">
<include name="etc/bin/test.bat"/>
</fileset>

Ant Non Recursive Fileset

Here is my directory structure:
module/
a/
foo.php
b/
bar.php
b/
c/
I would like to run a command for each directory under module/ but non recursively, so only these should be included:
a/
b/
c/
If I do this:
<target name="foo">
<apply executable="ls">
<arg value="-l" />
<fileset dir="${basedir}/module/">
</fileset>
</apply>
</target>
This will run recursively for each directory and file under module.
You only want to do this in the first level of directories?
<target name="foo">
<apply executable="ls">
<arg value="-l" />
<dirset dir="${basedir}/module/">
<include name="*"/>
</dirset>
</apply>
</target>
Note the <include>. I'm specifying only the directories immediately under the directory I specified in my <dirset/>. If I said, <include names="**/*"/>, it would specify all directories.
When you are dealing with directories and not files, use <dirset/> and not <fileset/>. <fileset/> is for specifying files. <dirset/> is for specifying directories.

Copy files and folder to WEB-INF using Ant

I want to copy a .properties file from a certain location to my WEB-INF/classes/com/infiniti folder(in a WAR file).
I have gone through this link How to get Ant to copy properties file to classes directory
using which I can copy the .properties file to WEB-INF/classes/ but not to WEB-INF/classes/com/infiniti
Code I am using is:
<war destfile="${deploy}/acc.war" webxml="${warSrc}/web/WEB-INF/web.xml">
<lib dir="${lib}">
.......
.......
.......
<classes dir="${configHome}/config/com/infiniti">
<include name="accredit.properties" />
</classes>
...
....
.......
</war>
Also I need to copy ${configHome}/resources/com/infiniti/errorcode folder to
WEB-INF/classes/com/infiniti.
Is this possible using Ant?
yes, you can use for instance ZipFileSet like this
<war destfile="${deploy}/acc.war" webxml="${warSrc}/web/WEB-INF/web.xml">
...
<zipfileset dir="${configHome}/config/com/infiniti" includes="**/*.properties" prefix="WEB-INF/classes/com/infiniti"/>
Yes, it's possible using ant. Just use the copy or sync commands to move your file:
<copy todir="${distribution}/location" file="${local.path}/data/file.txt">
</copy>
You can also copy with rules:
<copy includeemptydirs="false" todir="${combined.bin}">
<fileset dir="${buildbin}"/>
<fileset dir="${output2buildbin}"/>
<fileset dir="${output3buildbin}"/>
</copy>
Using sync:
<sync includeemptydirs="false" todir="${distres}">
<fileset dir="${buildres}">
<include name="logging/**" />
</fileset>
</sync>
Tasks are described on their doc site:
http://ant.apache.org/manual/Tasks/copy.html
The same 'fileset' declarative applies to the war task:
Examples
Assume the following structure in the project's base directory:
thirdparty/libs/jdbc1.jar
thirdparty/libs/jdbc2.jar
build/main/com/myco/myapp/Servlet.class
src/metadata/myapp.xml
src/html/myapp/index.html
src/jsp/myapp/front.jsp
src/graphics/images/gifs/small/logo.gif
src/graphics/images/gifs/large/logo.gif
then the war file myapp.war created with
<war destfile="myapp.war" webxml="src/metadata/myapp.xml">
<fileset dir="src/html/myapp"/>
<fileset dir="src/jsp/myapp"/>
<lib dir="thirdparty/libs">
<exclude name="jdbc1.jar"/>
</lib>
<classes dir="build/main"/>
<zipfileset dir="src/graphics/images/gifs"
prefix="images"/>
</war>
will consist of
WEB-INF/web.xml
WEB-INF/lib/jdbc2.jar
WEB-INF/classes/com/myco/myapp/Servlet.class
META-INF/MANIFEST.MF
index.html
front.jsp
images/small/logo.gif
images/large/logo.gif
http://ant.apache.org/manual/Tasks/war.html

How do I use Ant to copy a folder?

I'm trying to copy a directory using the Ant copy task.
I am a newbie at Ant; my current solution is:
<copy todir="${release_dir}/lib">
<fileset dir="${libpath}" />
</copy>
I'm wondering if there is a better and shorter way to accomplish the same thing?
First of all, those are the examples from Ant documentation:
Copy a directory to another directory
<copy todir="../new/dir">
<fileset dir="src_dir"/>
</copy>
Copy a set of files to a directory
<copy todir="../dest/dir">
<fileset dir="src_dir">
<exclude name="**/*.java"/>
</fileset>
</copy>
<copy todir="../dest/dir">
<fileset dir="src_dir" excludes="**/*.java"/>
</copy>
Copy a set of files to a directory, appending .bak to the file name on the fly
<copy todir="../backup/dir">
<fileset dir="src_dir"/>
<globmapper from="*" to="*.bak"/>
</copy>
Secondly, here is the whole documentation about copy task.
Just because the docs were not very clear to me, and because the time I spent can serve others:
The docs say that this "copies a directory (dir1) to another directory (dest)":
<copy todir="../new/dest">
<fileset dir="src/dir1"/>
</copy>
Actually, this does not mean "copy dir1 inside dest", but rather "copy the contents of dir1 inside dest".
(In general, in Ant, the "root dir" of a filesets -as well at the todir attribute- is not considered as being part of the set itself.)
To place the directory dir1 inside dest one has several alternatives (none totally satisfying to me - and I'd imagined that the new DirSet would help here, but no)
<copy todir="../new/dest/dir1">
<fileset dir="src/dir1"/>
</copy>
or
<copy todir="../new/dest">
<fileset dir="src" includes="dir1/**"/>
</copy>
See also here and here.
From http://ant.apache.org/manual/Tasks/copy.html:
<copy todir="../new/dir">
<fileset dir="src_dir"/>
</copy>
This will do it:
<copy todir="directory/to/copy/to">
<fileset dir="directory/to/copy/from"/>
</copy>
The ant manual is your friend: Ant Manual, in this case: Copy Task

Ant - copy only file not directory

I need to copy all files in a folder except directory in that folder using Ant script.
Im using below script to do that.
<copy todir="targetsir">
<fileset dir="srcdir">
<include name="**/*.*"/>
</fileset>
</copy>
But it copies all files and directory in that folder.
how to restrict/filter directory in that folder?
thanks,
I think there is an easier way.
flatten="true" - Ignore directory structure of source directory, copy all files into a single directory, specified by the todir attribute. The default is false.
Do you mean that srcdir conatins sub-directories, and you you don't want to copy them, you just want to copy the files one level beneath srcdir?
<copy todir="targetsir">
<fileset dir="srcdir">
<include name="*"/>
<type type="file"/>
</fileset>
</copy>
That should work. The "**/*.*" in your question means "every file under every sub directory". Just using "*" will just match the files under srcdir, not subdirectories.
Edited to exclude creation of empty subdirectories.
I do not have enough reputation to comment, so I'm writing new post here. Both solutions to include name="*" or name="*.*" work fine in general, but none of them is exactly what you might expect.
The first creates empty directories that are present in the source directory, since * matches the directory name as well. *.* works mostly because a convention that files have extension and directories not, but if you name your directory my.dir, this wildcard will create an empty directory with this name as well.
To do it properly, you can leverage the <type /> selector that <fileset /> accepts:
<copy todir="targetsir">
<fileset dir="srcdir">
<include name="*"/>
<type type="file"/>
</fileset>
</copy>
Can you try
<copy todir="targetsir">
<fileset dir="srcdir">
<include name="*.*"/>
</fileset>
</copy>
** is used to match a directory structure.
<copy todir="targetsir" includeEmptyDirs="false">
<fileset dir="srcdir">
<include name="*"/>
</fileset>
</copy>
If your folder has many subdirectories and you don't want them to be copied (if you want only files) try this..
<target name="copy">
<copy todir="out" flatten="true">
<fileset dir="tn">
<filename name="**/cit.txt" />
</fileset>
</copy>
</target>
The secret is to use not fileset but dirset instead.

Resources