Tricky Ant fileset (include most, but not all of a directory)

ant

I've got a case where I just can't figure out how to get Ant to pull in just the files I want in a fileset (why on earth didn't the Ant people just use regex?).

Here's the situation. I've got a bunch of classes under the classes directory. I want:

1) All of the files under classes,

2) But not those under /com/foo/bar

3) Except I do want /com/foo/bar/AbstractCustomerAdapter.class and CustomerAdapterFactory.class.

The files under /com/foo/bar are generally named things like FooCustomerAdapter.class.

In a nutshell, we create 2 jars, one with just the customer specific adpaters and one with everything else. This allows us to create a new customer adapter and swap out just that mini-jar. The fileset above that's killing me is the "everything else".

Once Ant excludes a file, no include will bring it back so items 2 & 3 are giving me heartache on how to do the fileset. How can I exclude all of my customer adapters, but not the abstract customer adapter? There's no ant-compatible wildcarding I can do to exclude the XZYCustomerAdapter classes without excluding the AbstractCustomerAdapter.

Well, at least I can't figure out one, but hopefully one of you Stack Overflow denizens is more clever than I 🙂

Best Answer

Most tasks that accept a single fileset can also accept multiple filesets.

<jar file="...">
  <fileset dir="classes">
    <exclude name="com/foo/bar/**"/>
  </fileset>

  <fileset dir="classes">
    <include name="com/foo/bar/AbstractCustomerAdapter.class"/>
    <include name="com/foo/bar/CustomerAdapterFactory.class"/>
  </fileset>
</jar>