ant – another neat tool

23
1 Ant – Another Neat Tool Ant – Another Neat Tool Representation and Management of Data on the Internet

Upload: zody

Post on 27-Jan-2016

58 views

Category:

Documents


0 download

DESCRIPTION

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 Based on industry standards (Java and XML) Open Source (development coordinated by the Apache Jakarta project). Make versus Ant. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Ant – Another Neat Tool

1

Ant – Another Neat ToolAnt – Another Neat Tool

Representation and

Management of Data on the

Internet

Page 2: Ant – Another Neat Tool

2

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)

Page 3: Ant – Another Neat Tool

3

Make versus AntMake versus Ant

• Make: OS dependent – uses shell commands

• Ant: OS independent – uses Java

• Make: Terrible syntax (infamous tabbing problem)

• Ant: XML based syntax

• Make: state dependencies between program files

• Ant: state dependencies between tasks (not between program files)

Page 4: Ant – Another Neat Tool

4

Why Ant?Why Ant?

• Platform independent

– Requires only a JDK 1.1 or later JVM

• Easy to use

– Built-in tasks accomplish all typical build

functions

– User contributed tasks cover most other needs

• Easy to extend

– Create a new task by writing some Java code

Page 5: Ant – Another Neat Tool

5

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

– send mail

– validate XML files

– etc. (anything you want)

Page 6: Ant – Another Neat Tool

6

Structure of AntStructure of Ant

• Project – a top level collection of targets

• Property– an Ant variable

• Target– a collection of tasks executed to achieve a

particular purpose (a goal)

• Task– a unit of Ant execution (a step)

Page 7: Ant – Another Neat Tool

7

How Does Ant Work?How Does Ant Work?

• Each Project will have a build file (build.xml)

• Each build file will contain one or more Targets

• The Target to be executed:– Is either explicitly selected on the command line

– Or a project default Target is executed

• Each Target is executed only once

• Each Target will contain one or more Tasks

• Some Tasks are executed conditionally

• Tasks are implemented as Java classes

Page 8: Ant – Another Neat Tool

8

Using AntUsing Ant

• Buildfiles are written in XML

• Each buildfile contains a single project

• Projects can have 3 attributes:

– name: name of project (optional)

– default: default target to use (required)

– basedir: base directory for paths

(optional)

Page 9: Ant – Another Neat Tool

9

A BuildFile – Project A BuildFile – Project ElementElement

<project name=“MyProject”

default=“compile”>

<!–- properties and targets will come

here...-->

</project>

XML Element

Comment

Page 10: Ant – Another Neat Tool

10

PropertiesProperties

• Properties (similar to global values) are

defined as follows:

<property name=“propName” value=“propVal” />

• Note: Properties are XML elements without

contents, therefore we use />

• A property “propName” can be referred to

later using the syntax ${propName}

• You can define any properties you want

Page 11: Ant – Another Neat Tool

11

A BuildFile – Adding A BuildFile – Adding PropertiesProperties

<project name=“MyProject” default=“compile”>

<property name=“buildDir” value=“build”/>

<property name=“srcDir” value=“.”/>

<!–- targets will come here...-->

</project>

Page 12: Ant – Another Neat Tool

12

TargetsTargets

• 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)

• Targets contain tasks as subelements. These tasks define the actions performed when the target is executed.

Page 13: Ant – Another Neat Tool

13

A BuildFile – Adding a A BuildFile – Adding a TargetTarget

<project name=“MyProject” default=“compile”>

<property name="buildDir" value="build"/>

<property name=“srcDir" value=“."/>

<target name="compile">

<javac srcdir="${src}" destdir="${build}"/> </target>

</project>

We call also have written:

<javac srcdir=“.“ destdir=“build"/>

A Task

Page 14: Ant – Another Neat Tool

14

A More Complex ExampleA More Complex Example

• Note: The tstamp task ( <tstamp/> )

defines the properties: DSTAMP (with

format “yyyymmdd”), TSTAMP (with

format “hhmm”) and TODAY (with

format “month day year”)

Page 15: Ant – Another Neat Tool

15

<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="init"> <!-- Create the time stamp --> <tstamp/> <!-- Create the build directory structure used by

compile --> <mkdir dir="${build}"/> </target>

Page 16: Ant – Another Neat Tool

16

<target name="compile" depends="init"> <!-- 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-${DSTAMP}.jar file --> <jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar" basedir="${build}"/>

</target>

<target name="clean"> <!-- Delete the ${build} and ${dist} directory trees --> <delete dir="${build}"/> <delete dir="${dist}"/>

</target> </project>

Page 17: Ant – Another Neat Tool

17

More about DependsMore about Depends

• Ant tries to execute the targets in

“depends” from left to right.

• However, a target may be executed

early when another one depends on it.

Page 18: Ant – Another Neat Tool

18

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!

Page 19: Ant – Another Neat Tool

19

Example 2Example 2

• Execute: ant A

• In what order will the tasks be

performed?

• The build fails, ant reacts with:

– “Circular dependancy: a <- b <- a”

<target name="A“ depends=“B”/> <target name="B" depends="A"/>

Page 20: Ant – Another Neat Tool

20

Running AntRunning Ant

• Type: ant

• Ant looks for the file: build.xml, and performs

the default task specified there.

• You can use the –buildfile option to specify a

different buildfile

• You can specify a different task to be

performed

• You can define parameters using the –D option

Page 21: Ant – Another Neat Tool

21

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

Page 22: Ant – Another Neat Tool

22

Examples (cont.)Examples (cont.)

• 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

Page 23: Ant – Another Neat Tool

23

ReferencesReferences

• To learn more about Ant:

– Look at the documentation on the web.

(reference from the table of lectures

schedule)

– Pay attention to the section: “Built-in

Tasks”. For each task, the format (e.g.,

name and attributes) appears.