Ant - PowerPoint PPT Presentation

About This Presentation
Title:

Ant

Description:

Based on industry standards (Java and XML) Open Source (development coordinated by the Apache Jakarta project) 3. Make versus Ant ... – PowerPoint PPT presentation

Number of Views:512
Avg rating:3.0/5.0
Slides: 24
Provided by: sarac
Category:
Tags: ant | apachebased

less

Transcript and Presenter's Notes

Title: Ant


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

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

3
Make 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)

4
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

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

6
Structure 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)

7
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

8
Using 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)

9
A BuildFile Project Element
  • ltproject nameMyProject defaultcompilegt
  • lt!- properties and targets will come here...--gt
  • lt/projectgt

XML Element
Comment
10
Properties
  • Properties (similar to global values) are defined
    as follows
  • ltproperty namepropName valuepropVal /gt
  • Note Properties are XML elements without
    contents, therefore we use /gt
  • A property propName can be referred to later
    using the syntax propName
  • You can define any properties you want

11
A BuildFile Adding Properties
  • ltproject nameMyProject defaultcompilegt
  • ltproperty namebuildDir valuebuild/gt
  • ltproperty namesrcDir value./gt
  • lt!- targets will come here...--gt
  • lt/projectgt

12
Targets
  • 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.

13
A BuildFile Adding a Target
  • ltproject nameMyProject defaultcompilegt
  • ltproperty name"buildDir" value"build"/gt
  • ltproperty namesrcDir" value."/gt
  • lttarget name"compile"gt
  • ltjavac srcdir"src" destdir"build"/gt
    lt/targetgt
  • lt/projectgt

A Task
We call also have written ltjavac srcdir.
destdirbuild"/gt
14
A More Complex Example
  • Note The tstamp task ( lttstamp/gt ) defines the
    properties DSTAMP (with format yyyymmdd),
    TSTAMP (with format hhmm) and TODAY (with
    format month day year)

15
  • ltproject name"MyProject" default"dist"
    basedir"."gt
  • lt!-- set global properties for this build --gt
  • ltproperty name"src" value"."/gt
  • ltproperty name"build" value"build"/gt
  • ltproperty name"dist" value"dist"/gt
  • lttarget name"init"gt
  • lt!-- Create the time stamp --gt
  • lttstamp/gt
  • lt!-- Create the build directory structure
    used by compile --gt
  • ltmkdir dir"build"/gt
  • lt/targetgt

16
  • lttarget name"compile" depends"init"gt
  • lt!-- Compile java code from src into
    build --gt
  • ltjavac srcdir"src" destdir"build"/gt
  • lt/targetgt
  • lttarget name"dist" depends"compile"gt
  • lt!-- Create the distribution directory --gt
  • ltmkdir dir"dist/lib"/gt
  • lt!-- Put everything in build into the jar
    file
  • MyProject-DSTAMP.jar file --gt
  • ltjar jarfile"dist/lib/MyProject-DSTAMP.ja
    r"
  • basedir"build"/gt
  • lt/targetgt
  • lttarget name"clean"gt
  • lt!-- Delete the build and dist directory
    trees --gt
  • ltdelete dir"build"/gt
  • ltdelete dir"dist"/gt
  • lt/targetgt

17
More 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.

18
Example 1
lttarget name"A"/gt lttarget name"B"
depends"A"/gt lttarget name"C" depends"B"/gt
lttarget name"D" depends"C,B,A"/gt
  • 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!
  • Note B is executed once!

Do D
Do C
Do B
Do A
19
Example 2
lttarget name"A dependsB/gt lttarget name"B"
depends"A"/gt
  • Execute ant A
  • In what order will the tasks be performed?
  • The build fails, ant reacts with
  • Circular dependancy a lt- b lt- a

20
Running 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

21
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

22
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 -Dbuildbuild/classes dist

23
References
  • 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.
Write a Comment
User Comments (0)
About PowerShow.com