File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

How to copy filelist or path?

Jim Mackraz
Greenhorn

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?

Thanks.
Jim Mackraz
Greenhorn

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}"/>
</path>

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

<mkdir dir="_dummydir"/>
<copy todir="_dummydir">
<fileset dir="."
includes="${mypath.property}"/>
</copy>


</target>
</project>

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

testcopy:
[echo]
[echo] myfile1 = foobar_1
[echo] myfile2 = foobar_2
[echo] mypath.property = E:\jim\junk\anttest\foobar_1 E:\jim\junk\anttest\foobar_2
[echo]

BUILD SUCCESSFUL
Total time: 1 second
$ ls _dummydir/
$
Jim Mackraz
Greenhorn

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">
<classpath>
<pathelement location="${ant-contrib.jar}" />
</classpath>
</taskdef>

<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"/>
</copy>

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

<!-- 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"/>
</foreach>
-->
</target>

<!-- 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}"/>
</target>
Carol Enderlin
drifter
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: http://aspose.com/file-tools
 
subject: How to copy filelist or path?