This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Ant, Maven and Other Build Tools and the fly likes Ant zip task - include empty directories, and subdirectories Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » Ant, Maven and Other Build Tools
Bookmark "Ant zip task - include empty directories, and subdirectories" Watch "Ant zip task - include empty directories, and subdirectories" New topic
Author

Ant zip task - include empty directories, and subdirectories

Joe Vahabzadeh
Ranch Hand

Joined: Jan 05, 2005
Posts: 140
All,

I've been driving myself insane trying to figure this one out - and going nuts with web searches. All to no avail.

First off, I am using ANT version 1.8.2, so I don't have the 1.6.2 issues with zip and empty folders.

I am trying to create a zip file, and my script so far is as follows:



So, what this does is take the jar file that exists in {dist} and add it to the zip file, so that when it's unzipped, the jar file comes out directly, and does NOT maintain the hierarchy (ie: doesn't create a {dist} directory and put the jar file inside it). This is the behavior I want so far.

HOWEVER, I would like to know how to do the following:
- include a {config} directory and ALL the files within it except for one called "test-config.xml"
- include two separate empty directories - say {subdir1} and {subdir2}. They may or may not have files in them, but I do NOT want ANY files in those directories when they get zipped.

For that second part with the empty directories, I'm perfectly content having the zip task create those in the zip itself without necessarily creating them on the local file system. In fact, the ideal would be if they'd be in the zip file and NOT be on the local file system - but I'll take whatever method I can get that works.

So, my local file system will look like so:

dist/MyJar.jar
config/config1.xml
config/config2.xml
config/test-config.xml
subdir1/ (may or may not exist and may or may not have files in it)
subdir2/ (may or may not exist and may or may not have files in it)


I want the ZIP file, when unzipped, to result in:
MyJar.jar
config/config1.xml
config/config2.xml
subdir1/
subdir2/


How do I accomplish this? I've tried variants of fileset, dirset, zipfileset, and am failing at every attempt.

Thanks in advance...
Roger Sterling
Ranch Hand

Joined: Apr 06, 2012
Posts: 426

You could run a script after the unzip that creates the empty directories you need. You may also file a bug against the Ant code. Some old Ant code may have snuck back into the code stream.
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5811
    
    7

Where are all of these files located before doing the zip? If they are spread out across the project's director structure, you will have to provide a <zipfileset> tag for each location. For example, something like this:



Another method, which I prefer, is to first use the <code> task to copy everything I want in the zip file to a common base directory and then zip up the base directory. I usually put those two tasks in two different targets, that way I can just build the directory structure, or just zip the files in the directory.

JBoss In Action
Joe Vahabzadeh
Ranch Hand

Joined: Jan 05, 2005
Posts: 140
D'oh! I couldn't see the forest for the trees...

I don't know what the "code" task is - but I assume you just meant to have another target in the build file that creates the structure that I wanted to exist in the zip file. I'm going to go with that method, it seems cleaner.

Seems way simpler now that I'm not trying to hammer a square peg into a round hole!

(it doesn't help that I actually was also screwing up my directory name property values . . having a name/location pair is great for setting up absolute directory paths, but using relative paths with the same name, I had to use name/value - forgot that little tidbit and drove myself batty in the process)
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5811
    
    7

Ooops, typo. <code> should be <copy>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Ant zip task - include empty directories, and subdirectories
 
Similar Threads
To set destination path
Ant build fail, sometimes 'cause ant forgets property
java.lang.NoClassDefFoundError: oracle/sql/ORADataFactory
Struts Validator problem
ANT Script Example for Websphere Application Server