tags:

views:

4463

answers:

3

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 :)

A: 

Write a custom selector (http://ant.apache.org/manual/CoreTypes/custom-programming.html). The api is easy to implement. There you have all the power of Java you need to decide.

Peter Kofler
Yea, I was really trying to avoid a custom selector simply to overcome Ant's lack of regex... :(. If I have to go that route, I'll just hand list the 50 files or so I need to exclude, ugly as that is.
Chris Kessel
A: 

I have two different things I do in different places. Sometimes I copy files to a temporary location so I can include everything plausible in one step and exclude some specific items in a later step. Other times, I use two separate statements.

You don't seem to need the temporary files, but in order to transform some text files to PC line ending before building an install, I copy and convert everything (using fixcrlf) relevant to a temporary directory, and then pick and choose files for different platforms from the created hierarchy.

When building tar files, it seems sufficient to be able to include a specific file on one line, and exclude the pattern on a later line that includes other files.

<tarfileset dir="classes" includes="classes" excludes="classes/com/foo/bar" />
<tarfileset dir="classes" includes="classes/com/foo/bar/AbstractCustomerAdapter.class,classes/com/foo/bar/CustomerAdapterFactory.class" />

If Jar files aren't constructed like tar and zip files, you may need to build the file hierarchy explicitly as I do with fixcrlf, and build the jar file in a second pass from the corrected hierarchy.

PanCrit
+4  A: 

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>
Sweet, that works!
Chris Kessel