Tuesday, January 14, 2014

ANT - JAVA CREATE JAR - Reference JARS dynamically

Example 1

<project name="DataLoader"
    basedir="."
    default="main">
    <property name="src.dir"
        value="src"/>
    <property name="build.dir"
        value="build"/>
    <property name="classes.dir"
        value="${build.dir}/classes"/>
    <property name="jar.dir"
        value="${build.dir}/jar"/>
    <property name="main-class"
        value="EntryPoint"/>
    <property name="lib.dir"
        value="libs"/>
    <!--     <path id="build.classpath"> -->
    <!--         <fileset dir="${lib.dir}" includes="**/*.jar"/> -->
    <!--     </path> -->
    <path id="build.classpath">
        <fileset dir="libs">
            <include name="**/*.jar"/>
        </fileset>
    </path>
    <target name="clean">
        <delete dir="${build.dir}"/>
    </target>
    <target name="compile">
        <mkdir dir="${classes.dir}"/>
        <mkdir dir="${jar.dir}"/>
        <javac srcdir="${src.dir}"
            destdir="${classes.dir}"
            classpathref="build.classpath"/>
    </target>
    <!--     <target name="run" depends="jar"> -->
    <!--         <java fork="true" classname="${main-class}"> -->
    <!--             <classpath> -->
    <!--                 <path refid="classpath"/> -->
    <!--                 <path location="${jar.dir}/${ant.project.name}.jar"/> -->
    <!--             </classpath> -->
    <!--         </java> -->
    <!--     </target> -->
    <target name="package"
        depends="compile">
        <echo>=== PACKAGE ===</echo>
        <!-- convert build.classpath to mf.classpath (the string needed for the manifest task) -->
        <pathconvert property="mf.classpath"
            pathsep=" ">
            <path refid="build.classpath"/>
            <flattenmapper/>
        </pathconvert>
        <!-- now build the manifest file, using mf.classpath -->
        <tstamp/>
        <!-- needed for TODAY -->
        <manifest file="MANIFEST.MF">
            <attribute name="Built-By"
                value="AbhinavSharma"/>
            <attribute name="Created-By"
                value="AbhinavSharma"/>
            <attribute name="Main-Class"
                value="${main-class}"/>
            <attribute name="Class-Path"
                value="${mf.classpath}"/>
        </manifest>
        <!-- create the jar file, including the manifest file we just created -->
        <jar basedir="${classes.dir}"
            destfile="${jar.dir}/Dol.jar"
            includes="**/*.*"
            excludes="**/*Test*"
            manifest="MANIFEST.MF"/>
        <!-- copy all the jar files out to the destination directory (dest.dir) -->
        <copy todir="${jar.dir}">
            <fileset dir="${lib.dir}">
                <exclude name="junit*"/>
                <include name="*.jar"/>
                <include name="*.zip"/>
            </fileset>
        </copy>
        <!-- move this file before the 'jar' task (and put it in the 'classes' dir) if you'd rather
             include it in the jar -->
        <!--         <copy file="${resources.dir}/log4j.properties" tofile="${dest.dir}/log4j.properties" overwrite="true" /> -->
        <!--         <copy file="${resources.dir}/${properties.file}" tofile="${dest.dir}/${properties.file}" overwrite="true" /> -->
        <!--         <delete dir="${dest.dir.classes}" /> -->
    </target>
    <target name="main"
        depends="clean, package"/>
</project>



Example 2



<!-- First, I create my classpath (build.classpath) from all the jar files in my lib directory -->
<path id="build.classpath">
    <fileset dir="libs">
        <include name="**/*.jar" />
    </fileset>
</path>
<!-- Next, my package task uses that classpath -->
<target name="package" depends="compile">
    <echo>=== PACKAGE ===</echo>
    <!-- convert build.classpath to mf.classpath (the string needed for the manifest task) -->
    <pathconvert property="mf.classpath" pathsep=" ">
        <path refid="build.classpath" />
        <flattenmapper />
    </pathconvert>
    <!-- now build the manifest file, using mf.classpath -->
    <tstamp/><!-- needed for TODAY -->
    <manifest file="MANIFEST.MF">
        <attribute name="Built-By" value="${manifest.built.by}"/>
        <attribute name="Created-By" value="${manifest.created.by}"/>
        <attribute name="Main-Class" value="${manifest.main.class}"/>
        <attribute name="Implementation-Version" value="${version.number}-b${build.number}"/>
        <attribute name="Built-Date" value="${TODAY}"/>
        <attribute name="Class-Path" value="${mf.classpath}" />
    </manifest>
    <!-- create the jar file, including the manifest file we just created -->
    <jar basedir="${dest.dir.classes}"
        destfile="${package.file}"
        includes="**/*.*"
        excludes="**/*Test*"
        manifest="MANIFEST.MF" />
    <!-- copy all the jar files out to the destination directory (dest.dir) -->
    <copy todir="${dest.dir}">
        <fileset dir="${lib.dir}">
            <exclude name="junit*" />
            <include name="*.jar"/>
            <include name="*.zip"/>
        </fileset>
    </copy>
    <!-- move this file before the 'jar' task (and put it in the 'classes' dir) if you'd rather
         include it in the jar -->
    <copy file="${resources.dir}/log4j.properties" tofile="${dest.dir}/log4j.properties" overwrite="true" />
    <copy file="${resources.dir}/${properties.file}" tofile="${dest.dir}/${properties.file}" overwrite="true" />
    <delete dir="${dest.dir.classes}" />
</target>




No comments:

Post a Comment