Sat, 17 Nov 07

The ant jar task and duplicate files can cause bizarre behavior and missing/incorrect files when unzipping

Posted in Java, Programming at 1:20 pm by moore | Permalink

I just ran into some bizarre behavior. I’m building a web application on Windows XPSP2, using Ant 1.6.1. The application worked fine. Then, I added one more copy instruction, to move a GWT component I’d just added to the appropriate place in the war file. Suddenly, the application ceased to work.

After a few hours of tracking down the issue, I found that it didn’t matter whether it was the new GWT component or an old one; it didn’t matter whether the copy went to the same directory or a new one–simply adding the new set of files caused the issue. Then I noticed that the unzipped version of the application differed. In particular, the configuration files differed. That explained why the application wasn’t working–it wasn’t configured correctly.

But, why were the configuration files different?

I examined the generated jar file. When I unjarred it, the configuration files were correct. I was using the jar command, whereas the ant script was using unzip. I made sure the jar file was copied correctly. I made sure the old directory was deleted, and that the ant unzip task would overwrite existing files. Still, no fix–I was seeing the incorrect configuration files.

Then, this part of the jar task documentation jumped out at me:

Please note that the zip format allows multiple files of the same fully-qualified name to exist within a single archive. This has been documented as causing various problems for unsuspecting users. If you wish to avoid this behavior you must set the duplicate attribute to a value other than its default, “add”.

The other possible values for the duplicate attribute to the jar task listed are “fail” and “preserve”. It doesn’t explain what the other options actually do; “fail” causes the jar task to fail when duplicate files are encountered. This seems to be sane default behavior, and I’m not sure why it’s not the case. “preserve” seems to preserve the first file added, and doesn’t add duplicates, but doesn’t tell you that duplicates exist.

Update, 2:09:   “preserve” does tell you that duplicates exist, in this form: WEB-INF/web.xml already added, skipping

I had, for a variety of reasons, a jar task that was adding two sets of the configuration files, with the same names and paths, to the war file. Something about adding a few extra files seemed to flip a switch, and change existing behavior. Whereas before, the unzip task picked the correct configuration file, now the unzip task picked the incorrect file. I don’t know more than that, because I didn’t dig down into the source.

The answer is to move the correct files to the top of the jar task, and change the “duplicate” attribute to be “preserve”.

I hope this post saves someone a few hours of banging their head.

Technorati Tags: , , ,

4 Comments »

  1. Chii said,

    March 21, 2008 at 2:31 am

    thanks, that was helpful! The default for jar should have been preserve - that is the one with the least surprise.

  2. Phil said,

    June 13, 2008 at 6:31 am

    Yeah, this bit us too. We were building a WAR file using Ant, and didn’t notice that duplicate files had been added until we tried deploying to WebSphere, which threw some weird “invalid archive” exception.

  3. Marc said,

    July 11, 2008 at 2:24 am

    thanks, for sharing this info. We also spend some hours searching for some a solution due to a ZIP double entry (SUN Java Applet Runtime did not download a JAR file due to a “verification error” because of a dublicate entry)

    IMHO duplicate=”preserve” should be the default, not “add”

  4. Paul bourdeaux said,

    August 22, 2008 at 9:33 am

    Thanks for blogging about this. I recently ran into the exact same problem. I agree completely with Marc that preserve should be the duplicate, not add. I gave you some link love on my blog regarding this:
    http://www.sundog.net/index.php/sunblog/entry/ant-and-duplicate-files-in-archives/

Leave a Comment

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-Spam Image

© Moore Consulting, 2003-2008