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
Related
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>
So I have a bunch of Jars in a directory that look like this:
library_2.4.3.jar 2/3/2012
library_3.0.1.jar 9/1/2012
api.lib_10.3.jar 3/2/2011
api.lib_12.4.5.jar 6/9/2012
I have already written the following using Ant 1.7 to copy the jars to where I want them and strip away the version number from the file
<copy todir="${lib.dir}" overwrite="true">
<fileset dir="${plugins.dir}">
<include name="library*.jar" />
<include name="api.lib*.jar" />
</fileset>
<regexpmapper from="(.*)_(.*).jar" to="\1.jar"/>
</copy>
The problem I'm running into is that I want it to copy the newer version of the file. Right now it seems to be copying only the older files. I have looked into the <sort> and <TimestampSelector> tasks but those are not supported under the copy task.
What can I do to copy the newer versions of the file?
Do not put them under the copy task directly... Create the property and use the property in the copy tag...
<timestampselector property="latest.modified">
<path>
<fileset dir="${my-directory.dir}">
<include name="file-*" />
</fileset>
</path>
</timestampselector>
<copy todir="." file="${latest.modified}">
Hope, it works.
Please see the blow script for doing the backup of current ear and replacing it with new version.
<move todir="/usr/local/jboss/server/default/ear_bk/" includeEmptyDirs="yes" verbose="true">
<fileset dir="/usr/local/jboss/release/server/default/deploy/tgr_10_10L_0036.ear" >
<include name="**/*" />
</fileset>
</move>
<copy todir="/usr/local/jboss/server/default/deploy/">
<fileset dir="/usr/local/jboss/release/server/default/deploy/tgr_10_10L_0037.ear"/>
</copy>
</target>
Issue:
1) Only the content of the tgr_10_10L_0036.ear is moved to ear_bk. How to move the tgr_10_10L_0036.ear?.
2) How to copy the complete tgr_10_10L_0037.ear directory to usr/local/jboss/server/default/deploy/ instead only the content ?
Simple quick fix is providing tgr_10_10L_0037.ear in todir
<copy todir="/usr/local/jboss/server/default/deploy/tgr_10_10L_0037.ear/">
<fileset dir="/usr/local/jboss/release/server/default/deploy/tgr_10_10L_0037.ear"/>
</copy>
for this you need to create tgr_10_10L_0037.ear before copying files
<mkdir dir="/usr/local/jboss/server/default/deploy/tgr_10_10L_0037.ear/"/>
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*/" />
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.