Amr Kamel
Amr Kamel

Reputation: 432

can't make jar with dependencies extracted in it via ant

I want to build my project using ant but I have a small problem. My problem is that I need the output jar have all my .class and all my jar dependencies extracted not zipped.

<project name="ivy example" default="compress" xmlns:ivy="antlib:org.apache.ivy.ant">

    <target name="resolve" description="Resolve and retrieve with ivy">
        <ivy:resolve />
        <ivy:cachepath pathid="compile.path" />
    </target>

    <target name="compile" depends="resolve" description="compilation">
       <mkdir dir="build/classes" />
       <javac srcdir="src" destdir="build/classes">
           <classpath refid="compile.path" />
       </javac>
    </target>

    <target name="compress"  depends="compile">
        <jar destfile="output/engine.jar" filesonly="true" update="true">
        <fileset dir="build/classes" />
        <fileset dir="lib"/>
            <manifest>
                <attribute name="Created-By" value="vireton"/>
                <attribute name="Main-Class" value="HelloIvy"/>
            </manifest>
        </jar>
        <echo>Building .jar file completed successfully!</echo>
    </target>

</project>

That code generates my engine.jar with the output classes + dependencies.jar. I want it to generate my classes and the dependencies extracted.

Can anyone help?

Upvotes: 1

Views: 711

Answers (3)

Ian Roberts
Ian Roberts

Reputation: 122424

You have your dependency jar files available as a path thanks to

    <ivy:cachepath pathid="compile.path" />

so the simplest way to bundle them all into your final jar would be (assuming Ant 1.8 or later)

  <jar destfile="output/engine.jar" filesonly="true" update="true">
    <fileset dir="build/classes" />
    <archives>
      <zips>
        <path refid="compile.path"/>
      </zips>
    </archives>
    <manifest>
        <attribute name="Created-By" value="vireton"/>
        <attribute name="Main-Class" value="HelloIvy"/>
    </manifest>
  </jar>

This will read the jars directly from your ivy cache, you don't need to retrieve them into a local lib directory first.

Upvotes: 0

Amr Kamel
Amr Kamel

Reputation: 432

i did it by extracting the dependencies to a tmp directory then make a jar of both directories (src & tmp)

  <target name="compress"  depends="compile">
<delete file="output/engine.jar" />
    <mkdir dir="tmp" />
    <unzip dest="tmp">
        <fileset dir="lib"> 
            <include name="*.jar" />
        </fileset>
    </unzip>
            <delete dir="tmp/META-INF" />
        <jar destfile="output/engine.jar" update="true">
        <fileset dir="build/classes" />
        <fileset dir="tmp"/>
            <manifest>
              <attribute name="Created-By" value="vireton"/>
              <attribute name="Main-Class" value="HelloIvy"/>
            </manifest>         
    </jar>  
     </target>

and i found an even better way to do it using

 <target name="compress"  depends="compile">
     <delete file="output/engine.jar" />
        <jar destfile="output/engine.jar" update="true">
        <zipgroupfileset dir="lib" includes="*.jar"/>
        <zipfileset dir="build/classes" />          
    </jar>  
   </target>

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 692281

Extract all the jars of the lib folder to some temporary folder (using the unjar task), then create the engine.jar by adding this temporary folder as a fileset.

Just like you would do it by hand.

Upvotes: 0

Related Questions