Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert an Ant Path into a FileSet?

Tags:

jakarta-ee

ant

I'm writing an Ant script to package a project into a WAR file. The software consists of several projects with their own source directories, libraries, etc.

The WAR task has a nested element lib which I'm currently working on. I currently have a reference of the required libs as a Path (containing several FileSets, which I use in a classpath reference. The lib, however, wants the input to be a FileSet, and it refuses a Path.

I tried converting my Path into a FileSet, but then I didn't get it to work as a classpath elsewhere.

Is there a way to convert a Path into a FileSet? I would hate to copy-paste the directories.

<path id="compile.libs">
    <fileset dir="${common.path}/lib" includes="*.jar"/>
    <fileset dir="${data.path}/lib" includes="*.jar"/>
    <fileset dir="${gui.path}/lib" includes="*.jar"/>
    <fileset dir="${gui.path}/WebContent/WEB-INF/lib" includes="*.jar"/>
</path>

...when used with <war ..><../> <lib refid="compile.libs"/> </war> leads to:

BUILD FAILED
build.xml:173: compile.libs doesn't denote a zipfileset or a fileset
like image 453
Henrik Paul Avatar asked Nov 09 '09 08:11

Henrik Paul


People also ask

What is fileset in Ant?

FileSets are groups of files. These files can be found in a directory tree starting in a base directory and are matched by patterns taken from a number of PatternSets. FileSets can appear inside tasks that support this feature or at the same level as target - i.e., as children of project .

What is PathElement in Ant?

This object represents a path as used by CLASSPATH or PATH environment variable. A path might also be described as a collection of unique filesystem resources. and PathElement: Helper class, holds the nested <pathelement> values.


2 Answers

Assuming the paths are absolute, you can first convert the Path to a comma-delimited list using <pathconvert>, and then convert the list back into a Fileset:

<!-- create path -->
<path id="foo.path">
    <pathelement location="/foo/bar/baz.txt"/>
    <pathelement location="/qux/quux/quuux.txt"/>
</path>

<!-- convert foo.path to foo.list -->
<pathconvert 
    refid="foo.path" 
    property="foo.list"
    pathsep=","
    dirsep="/"
>
    <!-- 
        <fileset> will want relative paths, so we need to strip 
        the leading /. result: "foo/bar/baz.txt,qux/quux/quuux.txt"
    -->
    <map from="/" to=""/> 
</pathconvert>

<!-- convert foo.list to fileset -->
<fileset id="foo.fileset" dir="/" includes="${foo.list}"/>

(Note the above assumes Unix; you may need to fiddle a bit with separators and whatnot if you're on Windows or you want to make it platform-independent.)

like image 174
David Moles Avatar answered Oct 07 '22 00:10

David Moles


You may have several choices.

  1. You may provide more than one <lib> nested element to <war> task. Maybe this would be enough.
  2. You may preassemble all of your lib files in one temporary directory and then just reference that directory as a fileset.
  3. There is an ant-contrib PathToFileSet task, but it requires a central root directory, and this may not be a case with your compile.libs layout.
  4. Since Ant 1.8.0 you can use a mappedresources. Source: Ant script: Prevent duplication of JAR in javac-classpath war-lib

I think I would try option 1.

like image 20
Alexander Pogrebnyak Avatar answered Oct 07 '22 00:10

Alexander Pogrebnyak