File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Ant, Maven and Other Build Tools and the fly likes How to copy filelist or path? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Ant, Maven and Other Build Tools
Bookmark "How to copy filelist or path?" Watch "How to copy filelist or path?" New topic

How to copy filelist or path?

Jim Mackraz

Joined: Mar 13, 2005
Posts: 3
I split my compile classpath definition up into two lists: the ones that I want to copy into my war file, and the ones that are just APIs and are implemented in Tomcat (like servlet-api.jar).

I'd like the target which builds my warfile to copy the first set of jars to the lib subdirectory of my build directory.

So, I have a filelist or path which contains a set of absolute pathnames of jar files. I can't figure out how to copy this set.

The copy task doesn't accept a filelist (just a fileset).

And using pathconvert create a property holding space- or comma-separated list of files doesn't help: you can't create a fileset of files using absolute path names in the "include" attribute... you have to specify a "dir" attribute, and it just doesn't find the files, because they are absolute and not relative to the fileset dir.

Any idea, other than <exec> to get a filelist copied to a directory?

Jim Mackraz

Joined: Mar 13, 2005
Posts: 3
To be perhaps more clear, consider this example. Create foobar_1 and foobar_2 in the same directory as this build.xml file, and try to get those to files to be copied into _dummydir. See in this example how relative paths to foobar_* become absolute pathnames in the path "mypath". From that point, I can see no way to copy them. Note that the "dir" attribute in the nested <fileset> is required, and these files aren't relative to that dir, as they expressed.

<project default="testcopy">

<target name="testcopy">
<property name="myfile1" value="foobar_1"/>
<property name="myfile2" value="foobar_2"/>

<path id="mypath">
<pathelement location="${myfile1}"/>
<pathelement location="${myfile2}"/>

<pathconvert property="" refid="mypath" pathsep=" "/>
myfile1 = ${myfile1}
myfile2 = ${myfile2} = ${}

<mkdir dir="_dummydir"/>
<copy todir="_dummydir">
<fileset dir="."


---------- output ----
$ ant
Buildfile: build.xml

[echo] myfile1 = foobar_1
[echo] myfile2 = foobar_2
[echo] = E:\jim\junk\anttest\foobar_1 E:\jim\junk\anttest\foobar_2

Total time: 1 second
$ ls _dummydir/
Jim Mackraz

Joined: Mar 13, 2005
Posts: 3
Well, I found a couple of methods using tasks in ant-contrib.

The first, using <pathtofileset> works if all the libs are under some base directory. The second, using <foreach>, works no matter where the lib jars are, but is probably (much) slower if you have a lot of jars. It will certainly clutter the output more...

<target name="warlibs" depends="init">

<!-- use the pathtofileset task in ant-contrib to copy
the jarfiles in lib.classpath. Note that
the jarfiles must all be "under" ${lib.dir}
<taskdef resource="net/sf/antcontrib/antlib.xml">
<pathelement location="${ant-contrib.jar}" />

<pathtofileset dir="${lib.dir}" pathrefid="lib.classpath"
ignorenonrelative="false" name="lib.fileset"/>

<copy verbose="true" flatten="true" todir="${build.lib.dir}">
<fileset refid="lib.fileset"/>

<!-- =================================================== -->

<!-- demonstration of another way to copy lib jars,
without the limitation of them lying under the lib.dir
or other base directory, required of pathtofileset target
<foreach target="copy-each-lib" param="_srcfile">
<path refid="lib.classpath"/>

<!-- part of 'foreach' method of copying lib jars -->
<target name="copy-each-lib">
<echo message="copy file ${_srcfile}"/>
<copy todir="${build.lib.dir}" file="${_srcfile}"/>
Carol Enderlin
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
You could take a look at using patternset. I include two patternsets in code block below and one copy using a fileset with the second patternset. Seems like that does what you want.

I agree. Here's the link:
subject: How to copy filelist or path?
jQuery in Action, 3rd edition