Thursday, April 4, 2013

Apache ant Build files


Apache ant Build files

 Introduction
Apache Ant is a Java library and command-line tool whose mission is to drive processes described in build files as targets and extension points dependent upon each other. The main known usage of Ant is the build of Java applications. Ant supplies a number of built-in tasks allowing to compile, assemble, test and run Java applications. Ant can also be used effectively to build non Java applications, for instance C or C++ applications. More generally, Ant can be used to pilot any type of process which can be described in terms of targets and tasks.

How install Apache Ant (on windows)


The Apache Ant distribution package can be downloading as a zip file from the Apache site and extract it to the location that Ant should install. Then, add the environment variables to the system as follow.

ANT_HOME=path to ant extracted folder
PATH=path to ant folder/bin



Open command prompt and type “ant” will give the result shown below. If this result will receive, then the installation is success.

Buildfile: build.xml does not exist!
Build failed


How work with Ant


In order to execute a build process with Ant, need a build script called “build.xml”. The build script is used to define where the library files are located, where the source code are located, how to compile, make directory , delete directory, make jar file, run jar files, run batch files and many more. In order to do this, it is required to know the basic commands in the Apache Ant. Following example shows the sample build script that compile a simple HelloWorld.java file and  run it.

<?xml version="1.0"?>
<project name="HelloWorld" default="run" basedir=".">
                <property name="src" location="src" />
                <property name="bin" location="bin" />
                <property name="build" location="build" />
                <target name="clean">
                                <delete dir="${bin}" />
                                <delete dir="${build}"/>
                </target>
                <target name="compile" depends="clean" >
<mkdir dir="${bin}" />
                                <javac destdir="${bin}" srcdir="${src}" debug="true"></javac>
                </target>
                <target name="jar" depends="compile">
                                <mkdir dir="${build}"/>
                                <mkdir dir="${build}/jar"/>
                                 <jar destfile="${build}/jar/HelloWorld.jar" basedir="${bin}">
                                                 <manifest>
                                                                <attribute name="Main-Class" value="HelloWorld"/>
                                                </manifest>
                                </jar>
                   </target>
<target name="run">
                                 <java jar="${build}/jar/HelloWorld.jar" fork="true"/>
                </target>
</project> 


 The steps of the build process (methods) can be defined inside the tag “target”. It is possible to give a name for each target and dependency. The dependency means, before run that target the dependent target will be run by the Ant. For an example, if execute compile target, since it depends on the clean target, the clean target will run first. Inside the “property” tag, the source directory, bin directory and other required paths with a logical name can be defined. The name of the project can be defined inside the “project” tag and also the default method that run, if required method does not specify when run the build script. The basedir is defined, from where the relative path should consider. For an example, the folder structure of the “HelloWorld” is as below.

 
                      Folder structure of the “HelloWorld” project

When executing clean target, if there is a directory called “bin” or “build”, that directory will be delete. When executing compile target, a directory will be created called “bin” and java file that inside the “src” folder, will be compiled and class files will be copied in to the bin folder. When executing “jar” target, a directory will be created called “build” and another directory will be created inside that called “jar”. Then the jar file will be created and the main class attribute will be added to the Manifest file of the jar. When executing “jar” target, the jar file will be run and print the result “Hello World”.

0 comments:

Post a Comment