Monday, 8 January 2007

Ant basics

Q: What is ANT?

Ø Java based build tool from Apache to create software products from source code
Ø De facto standard for building, packaging and installing etc of java applications
Ø It is configured with a build file that is an XML document
Ø Extended using Java classes






Q: How is it used?

Installation:

Ø Get the binary release from http://ant.apache.org/bindownload.cgi
Ø Set ANT_HOME environment variable
Ø Set JAVA_HOME environment variable
Ø Put the ANT bin directory on the PATH environment variable






Running:

Ø Type “ant” at the command line
Ø Automatically looks for the build.xml file in current directory to run
Ø Type “ant –buildfile ” to specify another buildfile to run
Ø Complete list of runtime parameters are
- buildfile buildfile – specify build file to use
- targetname – specify target to run (instead of running default)
- verbose, -quiet, -debug – Allows control over the logging information Ant outputs
- logger classname – Allows user to specify their own classes for logging Ant events










Q~: How buildfile is created or ANT tool is configured?

Each build file contains exactly one project and at least one target


Ø Project tag specify the basic project attributes and define the following
- name : name of the project
- default target : the default target to use when no target is specified
- basedir : the base directory from which all path calculations are done

Ø Each Target comprises one or more Tasks
Ø Task is a piece of executable java code (eg, java, jar etc)
Ø Tasks do the actual build work in ANT
Ø ANT has build in tasks and the ability to create new tasks
Ø Have the following structure
Ø Each Target comprises one or more Tasks
Ø Task is a piece of executable java code (eg, java, jar etc)
Ø Tasks do the actual build work in ANT
Ø ANT has build in tasks and the ability to create new tasks
Ø Have the following structure
Ø Target is a build module in ANT
- Each target contains task(s) that Ant to do
- One must be a project default
- Overall structure of targets:





Target tag specify the following attributes
- attribute = Description
- name* = the name of the Target
- depends = a comma separated list of targets on which this target depends
- if = name of the property that needs to be set in order for this target to execute
- unless = name of the property that must not be set in order for this target to execute
- description = a short description of this target’s function
Ø Each Target comprises one or more Tasks
Ø Task is a piece of executable java code (eg, java, jar etc)
Ø Tasks do the actual build work in ANT
Ø ANT has build in tasks and the ability to create new tasks
Ø Have the following structure

Ant overview: Writing own task (Task element)



Ø
Ø Each Target comprises one or more Tasks
Ø Task is a piece of executable java code (eg, java, jar etc)
Ø Tasks do the actual build work in ANT
Ø ANT has build in tasks and the ability to create new tasks
Ø Have the following structure Create a java class that extends org.apache.tools.ant.Task
Ø For each attribute, write a setter method that is public void and takes a single argument
Ø
Ø Each Target comprises one or more Tasks
Ø Task is a piece of executable java code (eg, java, jar etc)
Ø Tasks do the actual build work in ANT
Ø ANT has build in tasks and the ability to create new tasks
Ø Have the following structure
Ø Each Target comprises one or more Tasks
Ø Task is a piece of executable java code (eg, java, jar etc)
Ø Tasks do the actual build work in ANT
Ø ANT has build in tasks and the ability to create new tasks
Ø Have the following structure Write a public void execute() method, with no arguments, that throw a BuildException – this method implements the tasks itself


Properties

Ø Special task for setting up build file properties:
Example:

Ø Can use ${src} anywhere in build file to denote /home/src
Ø Ant provides access to all system properties as if defined by the task

Path Structures

Ø Ant provides means to set various environment variables like PATH and CLASSPATH.
Ø Example of setting CLASSPATH:





Integration with IDE

Most of the I DE’s like NetBeans, Eclipse, JBuilder, VisualAge, and almost any other Java IDE has Ant integration built in to the system
Refer to each IDE documentation to see how to use ANT with each IDE




Web development – ANT with Tomcat

Ø Tomcat comes with special Ant tasks to ease Web application development and deployment
Ø Copy $TOMCAT_HOME/server/lib/catalina-ant.jar to $ANT_HOME/lib
Ø Ant tasks for Tomcat:
- install
- reload
- deploy
- remove

No comments: