Wednesday, December 16, 2015

What is ANT TOOL? How it works in java Project?

Introduction:

Imagine that you are working on a large project. The project is a  java project and consists of many .java files. It consists of classes that are dependent on other classes which are drivers,they are situated in multiple directories and the output files must go to multiple directories too, you have various project build routes for different applications and at the moment are coordinating all of this manually or using some other build utility which doesn't do what you want it to so many hours are spent changing directories compiling individual files and so on.. Now imagine if there was a tool that could alleviate the and hassle you are experiencing .

ANT:
ant(originally an acronym for another Neat Tool), is a build tool with special support for the java programming language but can be used for just about everything. Ant is platform independent; it is written purely in java. Ant is particularly good at automating complicated respective tasks and thus is well suited for automating standardized build process.And accepts instructions in the form of XML documents and thus is extensible and easy to maintain.

Installing ANT:

Download the binaries fromhttp://jakartha.apache.org/ant/index.html,unzip them to a suitable directory.

  • Append/path/to/ant/bin to the path environment variable.

  • Append the .jar files in/path/to/ant/lib/to the CLASSPATH environment variable.
  • Set java_HOME to point to the location of JDK installation on the machine that the software is being installed on.   
  • Append/path/to/jdk/lib/ to the CLASSPATH environment variable.

Note: The installation instructions provided with the Ant software installation download are clear enough to warrant abstaining from writing any more about the installation here.Refer to/path/to/ant/does/manual/install.html.

Using Ant:

An Ant build file comes in the form of an XML document, all that is required is a simple text editor to edit the build file(s). An editor that provides XML syntax highlighting is  preferable. The ant installation comes with a JAXP-Compliant XML Parser,this means that the installation of an external XML parser is not necessary.

A simple Ant example is shown below followed by a set of instructions indicating how to use ant.

<?xml version="1.0"?>
<project name="test" default="compile"basedir=".">
            <property name="src" value=".">
            <property name="build"value="build"/>
            <target name="init">
               <mkdir dir="${build}"/>
                </target>
                 <!---compile the java code---->
               <target name="compile"depends="init">
                     <javac srcdir=${src}destdir="${build}"/>
                               </target>
                               </project>
HERE i am explaining each step one by one:

<?xml version="1.0">

   Since Ant build files are XML files the document begins with an XML declaration which specify which version of XML is in used,this is to allow for the possibility of automatic version recognition should it become necessary.

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

This is the root element of an ant build file is the project element , it has three attributes 
  • name: the name of the project,it can be any combination of alphanumeric characters that constitute valid XML.
  • default: The default target to use when no target is specified,out of these three attributes default is the only required attribute.
  • basedir: The base directory from which any relative directories used within the ant build file are referenced from.
<property name="scr" value=".">
<property name="build" value="build"/>

The property element allows the declaration of properties which are like user-definable variables available for use within an ant build file.The name attribute specifies the name of the property and the value attribute specifies the desired value of the property. The name and value values are subject to standard XML constraints.

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

The target element is used as a wrapper for a sequence of actions. A target has a name, so that it can be referenced from elsewhere,either externally from the command line,or internally via depend keyword,or through a direct call. The target in the example called "init"(initiate,it makes a directory using the mkdir element with the name specified by the build property defined in three.


Test.java:place the java file in the same directory as build.xml

public class test{
public static void main(String[]args){
System.out.println("hello world");
}
}

type the following  at the command line in the test directory:

ant -v
This will create a directory called build,compile test.java and place the .class file created in the build directory. The -c directs ant to be verbose. This verbosity causes the command to echo lots of information,information that is a not really necessary for most normal purposes.Execute the command sequence again. An example output message is shown below.
[javac]test.java omitted as /path/to/temp/build/test.class is uptodate.

A  nice feature if Ant is that by default,only those .java input files that have a more recent timestamps than their corresponding .class output files will be compiled.





No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...