3

If I have a folder with multiple subfolders (e.g. foo, bar, baz), how can I use ant to zip them individually into foo.zip, bar.zip and baz.zip, without doing them one by one?

Ideally there should also be a way to specify which files/subfolders to zip rather than "every single one".

2 Answers2

4

You don't want to use ant-contrib. ant-contrib sucks, every time you use it, you're incurring technical debt. The number one thing to keep in mind when using ant: let ant be ant. ant is what it is. it's declarative. The original intent of ant was simple declarations of properties and with the meat of the work being done in java (where you had a real IDE with a debugger and test frameworks). As noted in elements of ant style - "Let ant be ant". If you have to use ant-contrib, you should make your own ant task. If you can't do that, don't use ant. ant-contrib takes the worst of ant ant makes it worse, you'll soon find things unmaintainable. Enough ranting.

You don't even need to make a task for this one, this should work... it takes everything dir in /tmp and zips it up. should get you most of the way there.

    <mapper id="zip" type="glob"
    from="*"
    to="*.zip"/>

<target name="zip-craziness">
    <apply executable="zip">
        <targetfile/>
        <srcfile/>
        <dirset dir="/tmp" />
         <mapper refid="zip"/>
    </apply>
</target>
thekbb
  • 7,668
  • 1
  • 36
  • 61
  • 1
    Thanks, is there something like "apply" that runs ant tasks rather than external programs (without ant-contrib)? – aditsu quit because SE is EVIL Jun 10 '11 at 11:42
  • yes and no, the doesn't support a mapper or redirector - you could make a zip task that did. You're exactly right that it's sub-optimal to call out to the zip executable... it should nearly always work on your linux box, but you're going to have trouble on a pc. – thekbb Jun 10 '11 at 13:03
  • "the doesn't support a mapper or redirector" - the what? – aditsu quit because SE is EVIL Jun 10 '11 at 13:19
  • doh. that should have read: "the zip ant task doesn't support a mapper or redirector". I shouldn't even try to type before coffee. – thekbb Jun 10 '11 at 14:33
0

I used for-each from ant contrib for this kind of thing. You may able to improve on this question : Ant: How to execute a command for each file in directory?

Community
  • 1
  • 1
Jayan
  • 18,003
  • 15
  • 89
  • 143