Filter a fileset referenced using a refid

Mauli picture Mauli · Mar 3, 2009 · Viewed 17.6k times · Source

I have a fileset (which is returned from the Maven Ant task), and it contains all the jars I need to repack. This fileset is referenced by a refid. I only want to include our own jars, so I would like to filter that. But Ant filesets don't support any further attributes or nested tags if a refid is used.

For example, if the fileset is:

org.foo.1.jar
org.foo.2.jar
log4j.jar

and I want to have a fileset which contains only

org.foo*.jar

How would I do that?

Answer

Simon Lieschke picture Simon Lieschke · Mar 16, 2009

Try using a restrict resource collection, which you can use like a fileset in any task that uses resource collections to select the groups of files to operate on.

For example, for a fileset returned from your Maven task referenced via an id called dependency.fileset you can declare a restrict resource collection like so:

<restrict id="filtered.dependencies">
    <fileset refid="dependency.fileset"/>
    <rsel:name name="org.foo*.jar"/>
</restrict>

Note you'll have to declare the resource selector namespace as it isn't part of the built-in Ant namespace:

<project xmlns:rsel="antlib:org.apache.tools.ant.types.resources.selectors">
    ...
</project>

From here you can reference your restrict resource collection in a similar fashion to how you would reference your fileset. For example, to create backups of your filtered set of files:

<copy todir=".">
    <restrict refid="filtered.dependencies"/>
    <globmapper from="*" to="*.bak"/>
</copy>

Of course you can inline your restrict resource collection if you so desire:

<copy todir=".">
    <restrict>
        <fileset refid="dependency.fileset"/>
        <rsel:name name="org.foo*.jar"/>
    </restrict>
    <globmapper from="*" to="*.bak"/>
</copy>

Have a look at the Ant documentation on resource collections for further information.