ant Create jar package


Example

The following will create dist/output.jar from the source code in src and the libraries in lib, and will use src/Main.java as the main class.

<project name="Project" default="main" basedir=".">

    <property name="src.dir"     value="src"/>
    <property name="build.dir"   value="build"/>
    <property name="dist.dir"    value="dist"/>

    <path id="classpath">
        <fileset dir="lib" includes="**/*.jar"/>
        <pathelement path="${build.dir}"/>
    </path>

    <target name="clean">
      <delete dir="${build.dir}"/>
      <delete dir="${dist.dir}"/>
    </target>
    

    <target name="compile">
        <mkdir dir="${build.dir}"/>
        <javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="classpath"/>
        <copy todir="${build.dir}">
            <fileset dir="${src.dir}" excludes="**/*.java"/>
        </copy>
    </target>

    <target name="jar" depends="compile">
        <mkdir dir="${dist.dir}"/>
        <jar destfile="${dist.dir}/${ant.project.name}.jar" basedir="${build.dir}">
            <fileset dir="${build.dir}"/>
            <restrict>
                <archives>
                    <zips>
                        <fileset dir="lib" includes="**/*.jar"/>
                    </zips>
                </archives>
            </restrict>
            <manifest>
                <attribute name="Main-Class" value="Main"/>
            </manifest>
        </jar>
    </target>

    <target name="main" depends="clean,jar"/>
</project>