Monday, October 19, 2009

ANT “Hello World” Example

I thought sharing very basic ant build.xml file which will compile a single class and create a jar by adding manifest file to it. Let start explaining this step by step.
AntHelloWorld Java Class:

Create a java class with which will print hello world in the console.

package com.passion4java.helloworld.ant;
class AntHelloWorld {
    public static void main(String[] args) {
        System.out.println("This Project was build using Ant");
    }
}



Create a build.xml file which will first clean and compile the java classes and then build the jar file.
Hello world build script:
<?xml version="1.0" encoding="UTF-8"?>

<project name="AntHelloWorld" default="main-target" basedir=".">
<description>This is a simple build file to run this example file </description>
    <property name="source.dir" value="src"/>
    <property name="build.dir" value="build"/>
    <property name="classes.dir" value="${build.dir}/classes"/>
    <property name="main-class-name" value="com.passion4java.helloworld.ant.AntHelloWorld"/>
<target name="main-target" depends="clean,build" description="--> description">

    <!-- creating a jar file-->

    <jar basedir="${classes.dir}" destfile="${ant.project.name}.jar">

        <!-- adding the main class to the manifest file-->

        <manifest>

            <attribute name="Main-Class" value="${main-class-name}"/>

        </manifest>

    </jar>
    <delete dir="${build.dir}"/>

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

    <delete dir="${classes.dir}"/>
    <delete file="${ant.project.name}.jar"></delete>

</target>

    <!--target:build java classes    -->

    <target name="build">
    <mkdir dir="${build.dir}"/>

    <mkdir dir="${classes.dir}"/>

        <javac srcdir="${source.dir}" destdir="${classes.dir}"/>
    </target>

</project>


Execution:
Download the zip file which is attached and extract it. You will see the directory structure as below.


Go the folder where the build.xml was there and start execute the file by giving the ant build.xml from the command prompt.All the properties which we defined is relative so it should not have any path related issues



How it Works:

  • We have created a project called AntHelloWorld and we have created 3 targets and 4 properties.
  • The name of the Project can also referred by ${ant.project.name}
  • main-target is the default target which be executed when you are running build.xml file without specifying any targets.
  • Once the main-target get triggered, it will first finish all the depended targets which are defined in depends attribute of the target tags.
  • In our main target they are 2 depended targets clean,build. So both clean and build should be execution first before main-target starts.
  • The order as in depended targets should matters, because as you have the target listed based on that only the ant which will pick which one to execute first. In our case it should be clean target.
  • Clean- target deletes the folder and jar if they are present. If no folder is there this will not throw any exceptions.


  • Build targets just compiles all the java classes in the source directory and it will place all the compiled classes in the destination directory

    <javac srcdir="${source.dir}" destdir="${classes.dir}"/>

  • Then our main-target starts it calls the jar tags which pointing to the classes folder which was created by javac tag.
  • There is also a manifest tag which adds the manifest file to the jar file.
  • Main-Class attribute was added to the manifest file using the below tag <manifest>            <attribute name="Main-Class" value="${main-class-name}"/>        </manifest>
  • Main-Class is nothing but the entry point class for this jar.
    That's it, now we have a jar with a single class file and a manifest file inside in it :)  Download ANT HelloWorld Zip File


    No comments:

    Post a Comment

     

    This content comes from a hidden element on this page.

    The inline option preserves bound JavaScript events and changes, and it puts the content back where it came from when it is closed.
    Click me, it will be preserved!

    If you try to open a new ColorBox while it is already open, it will update itself with the new content.

    Updating Content Example:
    Click here to load new content