how to add a complete folder to a jar file by ant

16,879

Solution 1

If you specify to use the directory as the root for your file set then you can just match for the directory and it will preserve the structure.

<jar destfile="myjar.jar" >
  <fileset dir=".">
    <include name="**/${libs.dir}/**"/>
  </fileset>
</jar>

Solution 2

Use this ant extension: http://one-jar.sourceforge.net/

There is also Eclipse plugin: FatJar

Solution 3

You have to do something like the following. Specifically, the zipfileset command. You basically are saying you want to build the ${build.name}.jar (you could hard code a path to be "myjar.jar" or something along those lines) and then add the various files to the JAR.

Hope this helps!

<jar destfile="${dist}/${build.name}.jar">

    <!-- Generate the MANIFEST.MF file. -->
    <manifest>
        <attribute name="Built-By" value="${user.name}" />
        <attribute name="Release-Version" value="${version}" />
        <attribute name="Main-Class" value="my.lib.Main" />
            <attribute name="SplashScreen-Image" value="TitleScreen.png" />
        <attribute name="Class-Path" value="${classpath}" />
    </manifest>

    <zipfileset dir="${build.dir}" />
    <zipfileset dir="${resources}" />

    <fileset file="${resources}/icons/misc_icons/TitleScreen.png" />
</jar>
Share:
16,879
dm76
Author by

dm76

Updated on June 04, 2022

Comments

  • dm76
    dm76 almost 2 years

    I'd like to create like a "fat" jar with ant where I have, not only the usual classes, manifest files, etc, but also my 'libs' folder too.

    I tried with:

    <jar destfile="myjar.jar" update="yes" basedir="${libs.dir}"/>
    

    but this adds the files in 'libs' the root of the jar file where I'd like to have the libs folder itself in the jar (with everything it contains of course)

    Can I maybe create the lib folder myself in the jar and add the files to that specific location in the jar then ?