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.
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>
I have the following piece of code:
<jar destfile="${jar.file}">
<fileset dir="${basedir}/resources"/>
<include name="META-INF/ejb-jar.xml"/>
<include name="META-INF/persistence-prod.xml"/>
</fileset>
[...]
</jar>
The problem is that persistence-prod.xm1 should be persistence.xml when placed in the jar.
I know I could create a working directory and layout my whole jar there, and then jar that up. I know I can copy that one file elsewhere and rename it while copying. If I had a whole bunch of files named *-prod.xml to be renamed *.xml, I can use a file mapper inside the copy task. However, I want to be able to rename the file right in the <jar> task. I tried adding <globmapper> to the jar task, but I got the error message: jar doesn't support the nested "globmapper" element.
Any idea how this rename can take place while jaring the file?
Of course, the minute I asked the question, I figure out the answer:
I can't put <globmapper> directly into a <jar> task, but I can include <mappedresources> into the <jar> task, and place my <globmapper> in there:
Wrong:
<jar destfile="${jar.file}">
<fileset dir="${basedir}/resources"/>
<include name="META-INF/ejb-jar.xml"/>
<include name="META-INF/persistence-prod.xml"/>
</fileset>
<globmapper from="*-prod.xml" to="*.xml"/>
[...]
</jar>
Right:
<jar destfile="${jar.file}">
<fileset dir="${basedir}/resources"/>
<include name="META-INF/ejb-jar.xml"/>
</fileset>
<mappedresources>
<fileset dir="${basedir}/resources">
<include name="META-INF/persistence-prod.xml"/>
</fileset>
<globmapper from="*-prod.xml" to="*.xml"/>
</mappedresources>
[...]
</jar>
I guess this makes sense since it limits my file mapping to just the <mappedresources> and not to all <fileset> of the <jar> task.
My application folder structure looks like this...
-js
-libs
-jquery.js
-jquery-ui.js
-app.js
-ui.js
I wish to concatenate the .js files that are in the /js directory but not those in the /js/libs directory. I am using the following code, but it is ignoring the exclude statement:
<concat destfile="${build.dir}/js/foot-${build.major}-${build.minor}.concat.js">
<fileset dir="${build.dir}/js">
<exclude name="**/libs/**" />
</fileset>
</concat>
The <fileset> should specify both include and exclude patterns. In addition, it may be good to specify a destination directory for <concat> separate from the <fileset> input directory.
<target name="concat-test">
<concat destfile="${basedir}/concat.js" fixlastline="yes">
<fileset dir="${basedir}/js">
<include name="**/*.js" />
<exclude name="**/libs/**" />
</fileset>
</concat>
</target>
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'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