ANT – Another Neat Tool Representation and Management of Data on the Internet.
-
Upload
melvyn-green -
Category
Documents
-
view
218 -
download
0
Transcript of ANT – Another Neat Tool Representation and Management of Data on the Internet.
ANT – Another Neat ToolANT – Another Neat Tool
Representation and Management of
Data on the Internet
What is ANT?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?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 BuildfilesANT 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)
Buildfile ExampleBuildfile Example
<project name="MyProject" 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>
ProjectsProjects
• 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)
PropertiesProperties
• Properties (global values) are typically defined as follows:
<property name=“propName” value=“propVal”/>
• Note: Properties are XML elements without contents, therefore we use />
• A property named “propName” can be referred to later using the syntax: ${propName}
• You can define any properties you want
• Properties are not variable: the first value they are given remains for the whole build!
Special Property DefinitionsSpecial Property Definitions
• <property name="src" location="srcFile"/> - sets src to the absolute file name of srcFile, taken to be
relative to the project's basedir • <property file="pFile"/>
- reads a set of properties from a file called pFile• <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.name,
user.home, etc…
TasksTasks
• A task is a piece of code to be executed
• The general form of a task element is
<taskname param1="value1" param2="value2"... />
• 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 ExamplesSome Built-In Tasks Examples
• Directory tasks:
<mkdir dir="${workDir}"/>, <delete dir="${workDir}"/>
• 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 ExamplesMore Built-In Tasks Examples
• Java tasks:
<java classname="Ex4Main"/>
<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:
<exec executable="emacs">
<arg value="readme.txt"/>
</exec>
TargetsTargets
• 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 BuildFile – Adding a TargetA BuildFile – Adding a Target
<project name=“MyProject” default=“compile”>
<property name="buildDir" value="build"/>
<property name=“srcDir" value=“."/>
<target name="compile">
<javac srcdir="${srcDir}" destdir="$
{buildDir}"/> </target>
</project>We could also have written:
<javac srcdir=“.“ destdir=“build"/>
A Task
<project name="MyProject" 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 in ${build} into the jar file: MyProject.jar file --> <jar jarfile="${dist}/lib/MyProject.jar" basedir="${build}"/>
</target>
<target name="clean"> <!-- Delete the ${build} and ${dist} directory trees --> <delete dir="${build}"/> <delete dir="${dist}"/>
</target> </project>
More about DependsMore 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 1Example 1
• Execute: ant D
• In what order will the tasks be performed?
<target name="A"/> <target name="B" depends="A"/> <target name="C" depends="B"/> <target name="D" depends="C,B,A"/>
Try D Try C Try B Try A
Do D Do C Do B Do A
• Note: B is executed before C! • Note: B is executed once!
Example 2Example 2
• Execute: ant A
• In what order will the tasks be performed?
• The build fails, ant reacts with:
Circular dependency: A <- B <- A
<target name="A“ depends=“B”/> <target name="B" depends="A"/>
Running ANTRunning 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
ExamplesExamples
• 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 ANTMake 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)