How does the ant war task work?

I have a question about something I encountered today at work.

I have two projects that are built with ant build script.

Lets call them projectA and projectB.

ProjectA builds projectA.jar, and projectB builds projectB.war, and projectB.ear.

ProjectB.war includes projectA.jar in the web-inf lib directory.

In projectB there is a lib directory and it contains projectA.jar.

In this build scenario the build file for projectB pulls in the projectA.jar directly from projectA and it also pulls in projectA.jar from the projectB lib directory(I know bad practice, but I inherited this project, and am fixing it :)).

So when the war and subsequent ear file are built, in the web-inf lib directory of the projectb.war there are two projectA.jar files.

So the first question is:

How does ant put the war file together? I would think if it is using a temporary directory to store the files for packaging, I would not be able to have two files w/ the same name?

Second:

When I deploy this on OC4J, and look in the directory that contains the exploded ear/war files then there is only 1 projectA.jar file, as I would expect. How does it determine which file is used, not important? I'm just curious.

This was the source of problems as the projectA was building differently on different machines, another problem in itself. But the jar that was selected out of the two was always consistent, a build from one machine would always work and one from another machine would not.

thanks for your help.

13.10.2009 22:46:23
1 ОТВЕТ
РЕШЕНИЕ

A WAR is simply a zip file. And as such can contain two files with the same name. It is even says so in the Ant manual:

Please note that the Zip format allows multiple files of the same fully-qualified name to exist within a single archive.

Ant does not use a temporary directory to build the WAR. It's all done in memory.

Since in Windows two files with the same name cannot exist, one of them is ignored during extraction. I'm not sure which one is ignored, but my guess would be that the first one added is the only one extracted.

To avoid this kind of error, you need to specifically exclude one of the files:

<war destfile="myapp.war" webxml="src/metadata/myapp.xml">
  <lib dir="libs">
    <exclude name="projectA.jar"/>
  </lib>
  <lib dir="projectAlibs"/>
...
</war>

4
17.01.2011 00:08:22
Thanks, yeah, I excluded the file w/ no problems, I was just mainly curious about what was going on in the background with ant and the zip creation.
broschb 14.10.2009 01:17:51