Menu

Building With Ant

Daniel P. Dougherty

Ant is one of the most popular project build tools for JAVA applications. To use Ant, you will typically create and edit a build.xml file which lists instructions (called tasks) for Ant to carry out. Your Ant tasks will automate the linking, compiling, and even packaging your software.

What's especially nice about Ant is that it elegantly handles many of the complex classpath issues peculiar to JAVA development and deployment. Every build.xml is a plain text file so you will need to use your favorite text editor (e.g. jEdit, NEdit etc. ) to create and edit the file.

To get started, let's assume your username is jdoe and you want to produce version 1.0 of an application called HelloSnifflib. First, you will need to create a sandbox in which to play in.

To do this, create a directory file structure looking something like this.

  • antbuildexample/
    • build.xml
    • lib/
    • src/
    • src/com/
    • src/com/jdoe/
    • src/com/jdoe/demos/
    • src/com/jdoe/demos/HelloSnifflib.java

Where the file HelloSnifflib.java has been edited to look like this.

package com.jdoe.demos;

import com.mockturtlesolutions.snifflib.datatypes.DblMatrix;


/**
Basic hello world program linking to Snifflib numerical library.    
*/
public class HelloSnifflib
{
    public static void main(String[] args)
    {
        System.out.println("Hello World");
        DblMatrix X = new DblMatrix("[1 2 3; 4 5 6; 7 8 9]");
        X = X.times(DblMatrix.PI);
        X.show("Hello Snifflib!");
    }
}


Note the presence of the package line which indicates that this file belongs in the demos package of jdoe's application library.

<?xml version="1.0"?>

<project name="helloproject" default="compile" basedir=".">

    <!--
    Version of your software.
    -->
    <property name="releaseversion" value="1.0"/>

    <!--
    Specify where the source code lives. Change to suit your project's file tree.
    -->
    <property name="src" value="src"/>

    <!--
    Specify where build should happen.
    -->
    <property name="build" value="build"/>


    <!--
    Specify where build should happen.
    -->
    <property name="destjar" value="release"/>


    <!-- 
    Where we'll keep 3rd-party libraries (.jar) files.
    -->
    <property name="lib" value="lib"/>

    <!--
    In preparation for production of an executable jar file.
    -->
    <property name="jarmainclass" value="com.jdoe.demos.HelloSnifflib"/>

    <!-- 
    Might want to require/enforce particular version of JDK.
    -->
    <property name="sourceversion" value="5"/> 
    <property name="targetversion" value="5"/>


    <target name="init" description="Prep the production">  
        <mkdir dir="${build}"/>
        <mkdir dir="${destjar}"/>
    </target>

        <!--
    Prepare for a completely fresh build.
    -->
    <target name="cleanall" description="Remove previous build and release">
        <delete verbose="false" includeEmptyDirs="true">
            <fileset dir="${build}"/>
            <fileset dir="${destjar}"/>
        </delete>
    </target>

    <!--
    Prepare for a completely fresh build.
    -->
    <target name="cleanbuild" description="Removes previous build">
        <delete verbose="false">
            <fileset dir="${build}"/>
        </delete>
    </target>


    <!--
    Where will needed object (.class) and .jar files be found?
    -->
    <path id="compile.classes">
            <pathelement path="${build}"/>
            <fileset dir="${lib}" casesensitive="yes">
                <include name="**/*.jar"/>
            </fileset>
    </path>


    <target name="compile" depends="init" description="Compiles the java code"> 
            <javac  srcdir="${src}"
                includeantruntime="false"
                classpathref="compile.classes"
                destdir="${build}"
                deprecation="true"
                debug="true"
                source="${sourceversion}"
                target="${targetversion}">
            </javac>
    </target>

    <target name="makejar" description="Produce Executable JAR" depends="compile">
        <unjar dest="${build}">
            <fileset dir="${lib}">
                <include name="**/*.jar"/>
            </fileset> 
        </unjar>

        <jar destfile="${destjar}/HelloSnifflib-${releaseversion}.jar" basedir="${build}">
            <manifest >
                <attribute name="Built-By" value="${user.name}"/>
                <attribute name="Main-Class" value="${jarmainclass}"/>

            </manifest>
        </jar>
    </target>


</project>


Before you can run the build, you'll want to save the latest version of the Snifflib library jar to your lib folder.

Now that you have your build.xml file created and saved, you can find out what your build allows. Try the following at your command line:

ant -p


You should see a description of all the ant tasks you can now invoke:

Main targets:
    cleanall    Removes previous build and release
    cleanbuild  Removes previous build
    compile     Compiles the java code
    init        Prep the production
    makejar     Produce Executable JAR


To build the executable jar for your application, simply execute

ant makejar


To run the application, simply double-click on the release/HelloSnifflib-1.0.jar which should now be in the release folder. Alternatively, to run the application from the command line you may do:

java -jar ./release/HelloSnifflib-1.0.jar


The result should be:

Hello World

[Hello Snifflib!] = 
        3.142    6.283    9.425
        12.566    15.708    18.850
        21.991    25.133    28.274

Related

Wiki: Home
Wiki: Your First Repository Framework App

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.