Ant

From Suhrid.net Wiki
Jump to navigationJump to search

Intro

  • Ant is designed primarily for building Java projects, but that's not its only use.
  • It is helpful for other tasks, such as performing filesystem operations in a cross-platform way.
  • As an application's build process becomes more complex, it becomes increasingly important to ensure that precisely the same build steps are carried out during each build, with as much automation as possible, in order to produce consistent builds in a timely manner.
  • Ant is similar to make in that it defines dependencies between build tasks; however, instead of implementing build tasks using platform-specific shell commands, it uses cross-platform Java classes. With Ant, you can write a single build file that operates consistently on any Java platform (as Ant itself is implemented in the Java language); this is Ant's greatest strength.

Build File

  • Build files are written in XML.
  • Every build file contains a single top-level project element.
  • The project element consists of a number of target elements. A target is a defined step in a build that performs any number of operations, such as compiling a set of source files.
  • The operations themselves are performed by other specialized task tags. These tasks are then grouped together into target elements, as desired.
  • All of the operations required for a build could be placed into a single target element, but that would reduce flexibility. It is usually preferable to split the operations into logical build steps, with each

step contained in its own target element

  • Sample build file:
<?xml version="1.0"?>
<project default="init">
    <target name="init">
    </target>
</project>

Properties

  • Properties are like variables. They can be given a name and a value. However, they are immutable, the value cannot be changed once set.
  • Example:
<property name="db-driver" value="org.xyz.db.JDBCDriver"/>
<property name="db-conn" value="${db-driver}@host:port"/>

Dependencies

  • .Instead of specifying the targets in sequence order, Ant takes the more flexible approach of defining dependencies.
  • Each target is defined in terms of all the other targets that need to be completed before it can be performed. This is done using the depends attribute of the target element.
  • Depends means on "Depends-On"
  • Example:
<target name="init"/>
<target name="preprocess" depends="init"/>
<target name="compile" depends="init,preprocess"/>
<target name="package" depends="compile"/>