4

Possible Duplicate:
Clean way to combine multiple jars? Preferably using Ant

When I export a jar file from eclipse one of the options I get is to extract the class files from dependency jars into my final jar (other options include to just pack the jars into the my final jar or just leave everything outside of my final jar). There is of course a warning about licensing issues.

So here's the question -- what is the equivilant of this in apache ant? How can I make ant extract classes from jars that my program depends on and repack them into my final jar?

Community
  • 1
  • 1
knpwrs
  • 15,691
  • 12
  • 62
  • 103

1 Answers1

0

Try something like this:

<?xml version="1.0"?>
<project name="blah" basedir="." default="jar">
<target name="init">
  <property name="build" value="./build" />
  <property name="srcdir" value="./src" />
  <property name="lib.dir" value="./lib" />
  <property name="dist" value="./dist" />
  <property name="docs" value="./docs" />
  <property name="debug" value="true" />
  <path id="project.class.path">
    <pathelement path="${java.class.path}" />
    <fileset dir="${lib.dir}">
      <include name="*.jar" />
    </fileset>
  </path>
</target>
 <target name="compile" depends="init">
   <mkdir dir="${build}" />
   <javac debug="${debug}"
          optimize="${optimize}"
          destdir="${build}"
          deprecation="${deprecation}"
          source="1.6" target="1.6">
     <src path="${srcdir}" />
     <classpath refid="project.class.path" />
   </javac>
 </target>
 <target name="jar" depends="compile">
   <mkdir dir="${dist}" />
   <jar destfile="${dist}/${ant.project.name}.jar">
     <fileset dir="${build}">
       <include name="com/blah/blah/*.class" />
     </fileset>
   </jar>
 </target>
<target name="package" depends="compile, jar">
  <jar destfile="${dist}/${ant.project.name}-release.jar" basedir="${build}" includes="**/*.*">
    <manifest>
       <attribute name="Main-Class" value="com.blah.blah.Main" />
    </manifest>
    <zipgroupfileset dir="${lib.dir}" includes="*.jar" />
    <zipgroupfileset dir="${dist}" includes="${ant.project.name}.jar" />
  </jar>
</target>
</project>

You would then go:

ant package

To generate a single jar file with all the classes in the lib folder.

Femi
  • 64,273
  • 8
  • 118
  • 148