ANT Another Neat Tool Representation and Management of

  • Slides: 23
Download presentation
ANT – Another Neat Tool Representation and Management of Data on the Internet

ANT – Another Neat Tool Representation and Management of Data on the Internet

What is ANT? • A cross-platform build tool (like make) • A scripting framework

What is ANT? • A cross-platform build tool (like make) • A scripting framework • Based on industry standards (Java and XML) • Open Source (development coordinated by the Apache Jakarta project)

What can we do with ANT? • Can be used to: - compile java

What can we do with ANT? • Can be used to: - compile java programs - create javadoc documentation - create jar, zip, tar, war files - delete and copy files - validate XML files - etc. (send mail, anything you want)

ANT Buildfiles • An ANT buildfile is a file that contains the instructions for

ANT Buildfiles • An ANT buildfile is a file that contains the instructions for ANT’s tasks • A buildfile is written in XML • A buildfile has the following elements: - Project - a top level collection of targets - Property - an ANT variable - Target - a collection of tasks - Task - a unit of ANT execution (a step)

ANT Buildfiles project property. . . property Target Task . . . Target Task

ANT Buildfiles project property. . . property Target Task . . . Target Task

Buildfile Example <project name="My. Project" default="dist" basedir=". "> <property name="src" value=“. "/> <property name="build"

Buildfile Example <project name="My. Project" default="dist" basedir=". "> <property name="src" value=“. "/> <property name="build" location="build"/> <target name="compile“ description="compile the source"> <!-- Compile the java code from ${src} into ${build} --> <javac srcdir="${src}" destdir="${build}"/> </target> <target name=“jar" description=“build the JAR file"> <jar destfile="${dist}/lib/app. jar"basedir="${build}/classes"/> </target> <target name="clean" description="clean up"> <!-- Delete the ${build} and ${dist} directory trees --> <delete dir="${build}"/><delete dir="${dist}"/> </target> </project>

Projects • The project is the root element of the buildfile - One project

Projects • The project is the root element of the buildfile - One project per buildfile • Projects can have 3 attributes: - name: name of project (optional) - default: default target to use (required) - basedir: base directory for paths (optional)

Properties • Properties (global values) are typically defined as follows: <property name=“prop. Name” value=“prop.

Properties • Properties (global values) are typically defined as follows: <property name=“prop. Name” value=“prop. Val”/> • Note: Properties are XML elements without contents, therefore we use /> • A property named “prop. Name” can be referred to later using the syntax: ${prop. Name} • You can define any properties you want • Properties are not variable: the first value they are given remains for the whole build!

Special Property Definitions • <property name="src" location="src. File"/> - sets src to the absolute

Special Property Definitions • <property name="src" location="src. File"/> - sets src to the absolute file name of src. File, taken to be relative to the project's basedir • <property file="p. File"/> - reads a set of properties from a file called p. File • <property environment="env"/> - Later, you may use ${env. VAR_NAME}$ to get the system’s environment variable called VAR_NAME • Built-in Properties (predefined): - ant. file, ant. java. version, os. name, user. home, etc…

Tasks • A task is a piece of code to be executed • The

Tasks • A task is a piece of code to be executed • The general form of a task element is <taskname param 1="value 1" param 2="value 2". . . /> • ANT comes with some built-in tasks, which cover most of the basic needs for development of applications • One can also define new tasks (not covered here)

Some Built-In Tasks Examples • Directory tasks: <mkdir dir="${work. Dir}"/>, <delete dir="${work. Dir}"/> •

Some Built-In Tasks Examples • Directory tasks: <mkdir dir="${work. Dir}"/>, <delete dir="${work. Dir}"/> • Archive tasks: <jar jarfile="archive. jar" basedir="${build. classes}"/> <tar tarfile="archive. tar" basedir="src"/> <gzip zipfile="archive. tar. gz" src="archive. tar"/> • Output tasks: <echo message="Hello, ${user. name}"/>

More Built-In Tasks Examples • Java tasks: <java classname="Ex 4 Main"/> <java classname="Main"><arg value="-h"/>

More Built-In Tasks Examples • Java tasks: <java classname="Ex 4 Main"/> <java classname="Main"><arg value="-h"/> </java> <javac srcdir="${src}" destdir="${build}"/> - compiles only files that need to be compiled (time based) • Invoking external programs: <executable="emacs"> <arg value="readme. txt"/> </exec>

Targets • A target is a collection of tasks to be performed when the

Targets • A target is a collection of tasks to be performed when the target is executed • Targets have the attributes: - name: name of the target (required) - depends: comma separated list of targets on which the target depends (optional) - if, unless, description: details omitted (read about it in the ANT documentation)

A Build. File – Adding a Target <project name=“My. Project” default=“compile”> <property name="build. Dir"

A Build. File – Adding a Target <project name=“My. Project” default=“compile”> <property name="build. Dir" value="build"/> <property name=“src. Dir" value=“. "/> A Task <target name="compile"> <javac srcdir="${src. Dir}" destdir="${build. Dir}"/> </target> </project> We could also have written: <javac srcdir=“. “ destdir=“build"/>

<project name="My. Project" default="dist" basedir=". "> <!-- set global properties for this build -->

<project name="My. Project" default="dist" basedir=". "> <!-- set global properties for this build --> <property name="src" value=". "/> <property name="build" value="build"/> <property name="dist" value="dist"/> <target name="compile"> <!-- Compile java code from ${src} into ${build} --> <javac srcdir="${src}" destdir="${build}"/> </target>

<target name="dist" depends="compile"> <!-- Create the distribution directory --> <mkdir dir="${dist}/lib"/> <!-- Put everything

<target name="dist" depends="compile"> <!-- Create the distribution directory --> <mkdir dir="${dist}/lib"/> <!-- Put everything in ${build} into the jar file: My. Project. jar file --> <jar jarfile="${dist}/lib/My. Project. jar" basedir="${build}"/> </target> <target name="clean"> <!-- Delete the ${build} and ${dist} directory trees --> <delete dir="${build}"/> <delete dir="${dist}"/> </target> </project>

More about Depends • ANT tries to execute the targets in “depends” from left

More about Depends • ANT tries to execute the targets in “depends” from left to right • However, a target may be executed earlier when another one depends on it

Example 1 <target name="A"/> <target name="B" depends="A"/> <target name="C" depends="B"/> <target name="D" depends="C, B,

Example 1 <target name="A"/> <target name="B" depends="A"/> <target name="C" depends="B"/> <target name="D" depends="C, B, A"/> • Execute: ant D • In what order will the tasks be performed? Try D Try C Try B Try A • Note: B is executed before C! Do D Do C Do B Do A • Note: B is executed once!

Example 2 <target name="A“ depends=“B”/> <target name="B" depends="A"/> • Execute: ant A • In

Example 2 <target name="A“ depends=“B”/> <target name="B" depends="A"/> • Execute: ant A • In what order will the tasks be performed? • The build fails, ant reacts with: Circular dependency: A <- B <- A

Running ANT • Type: ant • ANT looks for the file: build. xml, and

Running ANT • Type: ant • ANT looks for the file: build. xml, and performs the default task specified there • Use the –buildfile option to specify a different buildfile • You can specify a different target to be performed • You can define parameters using the –D option

Examples • Run ANT using build. xml on the default target ant • Run

Examples • Run ANT using build. xml on the default target ant • Run ANT using the test. xml file on the default target ant -buildfile test. xml • Run ANT using the test. xml file on a target called dist: ant -buildfile test. xml dist • Run ANT using the test. xml file on a target called dist, setting the build property to the value build/classes: ant -buildfile test. xml -Dbuild=build/classes dist

Make versus ANT • Make: OS dependent -tasks are shell commands - Runs fast

Make versus ANT • Make: OS dependent -tasks are shell commands - Runs fast for small tasks • ANT: OS independent -tasks implemented as Java classes - requires Java (≥ JDK 1. 2) - Slow for small tasks – requires JVM • Make: Non-standard syntax (infamous tabbing problem) • ANT: XML based syntax • Make: state dependencies between program files • ANT: state dependencies between tasks (not between program files)

References • To learn more about ANT: - Look at the documentation on the

References • To learn more about ANT: - Look at the documentation on the web (reference from the table of lecture schedule) - Pay attention to the section Built-in Tasks: there are many tasks and task parameters you might find useful