views:

2021

answers:

2

I'm trying to build a web application which takes it's class files from multiple locations. These locations can contain the same class files. I need to be able to specify a priority so that some locations take priority over others when copying the classes across.

We have separate ant scripts which build the WAR file. This build is to hotswap in any changed classes whilst I am developing. So this build must be quick.

For example, my two class locations are:

  • /bin
  • /build/classes

I want classes from both these directories to be copied to: /web/WEB-INF/classes

However, if both these locations contain the same class file, eg:

  • /bin/com/ben/Test.class
  • /build/classes/com/ben/Test.class

I want files in /bin to always take priority.

So in the example:

  • the file: /bin/com/ben/Test.class, would be copied.
  • the file: /build/classes/com/ben/Test.class will be ignored.

My current script looks like this:

 <sync todir="${deploy}/WEB-INF/classes" verbose="true">
  <fileset dir="bin"/>
  <fileset dir="build/classes"/>
 </sync>

This works, but each time the script is run, dangling files are removed. I'm also not sure if any priority here is guaranteed.

Any assistance would be appreciated.

+1  A: 

By dangling files being removed, do you mean files which are already in your "${deploy}/WEB-INF/classes" directory?

Sync will clear existing files in the target directory, if you don't want that to happen, I would recomend using copy instead.

As for having a folder with a higher priority, you could just copy multiple times and overwrite existing files.

  <copy todir="${deploy}/WEB-INF/classes" verbose="true">
    <fileset dir="build/classes"/>
  </copy>

  <copy todir="${deploy}/WEB-INF/classes" verbose="true"  overwrite="true">
    <fileset dir="bin"/>
  </copy>

Now, test.class will be copied from build/classes then overwritten by test.class from bin.

Spike
Thanks Spike, I was trying to avoid copying the duplicate files over twice. Although it probably doesn't matter too much in terms of speed.
+1  A: 

It sounds to me like a very bad idea because if you have problems they will be time consuming to find out. But if you really want to do it here's a possible solution

<war [...] duplicate="preserve">
    [...]
    <classes dir="bin"/>
    <classes dir="build/classes"/>
</war>
Vladimir
Thanks Vladimir. I'm guessing that the WAR task will run the whole packaging each time it is run? I should have mentioned in the question text, the purpose of this build is as a hotswapping task to swap in changed files into the build. So the build must be fast. I'll try the war task.