2

I have a library that is typically distributed by making a zip file of the JAR and its dependencies and its javadoc by hand. I would like to automate this task in ant.

One quirk for the intended use case for this distribution is that when it is unpacked, the JAR my team has created and any library JARs should all be in the same path. We cannot have myproject.zip/the.jar and myproject.zip/lib/a_library.jar both should be in the root path of the zip.

I have had much success using the following task:

<target name="myproject.distributable" depends="artifact.mycompany_myproject, myproject.javadoc"
            description="Build the distributable JAR for myproject">
        <zip destfile="${basedir}/dist/myproject.zip">
            <fileset file="${temp.jar.path.mycompany_myproject.jar}"/>
            <zipfileset dir="mycompany_myproject/lib" prefix="lib">
                <patternset id="myproject.dist.libs">
                    <include name ="**/*.jar"/>
                </patternset>
            </zipfileset>
            <zipfileset dir="docs/myproject" prefix="docs"/>
        </zip>
    </target>

The only thing it doesn't do is 'flatten' or move the library JARs to the root path of the zip.

I have tried using <zipfileset prefix="/"> for the libs but that did not work as expected.

Freiheit
  • 8,408
  • 6
  • 59
  • 101
  • your code isn't appearing in the post. You need to add an extra newline after "the following task:" - I can't add it since any edits I make can't be just whitespace. – matt Apr 27 '11 at 15:15
  • Something like a `` would work, but that can't be used on fileset. http://ant.apache.org/manual/Types/mapper – Freiheit Apr 27 '11 at 17:37

2 Answers2

8

The prefix attribute of the zipfileset is used to describe where the files should appear in the created zip file. Since you want the jar files to appear at the root of the zip file you don't need to specify this, and can leave it out (I'm not sure what the effect of setting it to "/" will be, I think it'll be safer to omit it).

You problem seems to be that your libs are stored in subdirectories under your lib dir, but you want them to be directly in the root of the zip file. The 'zip' task, unlike the copy task, doesn't accept a mapper directly to change how files should appear in the zip, but if you're using ant 1.7 or later it will accept a resource collection. You can use a mappedresources element with a fileset and a flattenmapper to get the effect you're after:

<target name="myproject.distributable" depends="artifact.mycompany_myproject, myproject.javadoc" description="Build the distributable JAR for myproject">
  <zip destfile="${basedir}/dist/myproject.zip">
    <fileset file="${temp.jar.path.mycompany_myproject.jar}"/>
    <mappedresources>
      <fileset dir="mycompany_myproject/lib" includes="**/*.jar" />
      <flattenmapper />
    </mappedresources>
    <zipfileset dir="docs/myproject" prefix="docs"/>
  </zip>
</target>

This means you don't have to use copy first to put the jars into a staging area.

matt
  • 78,533
  • 8
  • 163
  • 197
0

According to this post the zipgroupfileset should do the trick; have not tried it myself though...

Community
  • 1
  • 1
Stijn Geukens
  • 15,454
  • 8
  • 66
  • 101
  • I tried the following: `` and I did not get any JARs. Then I tried `` and got something like `ApacheJakarta/some.jar`. – Freiheit Apr 27 '11 at 17:11
  • May have solved this myself. I had to use a `copy` task with the flatten option set to true. Copy moved everything to a staging path and then I fed that staging path to my zip task. – Freiheit Apr 27 '11 at 18:40