SlideShare a Scribd company logo
Introduction to Ant
      @brownylin
Why

• Many IDEs have their own build
  systems, so why is Ant important to
  learn and use


• Answer:
 ‣ Eclipse may actually be using Ant
 ‣ Not just a build tool, must have for automation
What is Ant?



• Ant   Java
               Make    Make
           Java
Outline



•A First Ant Build
• Ant and Eclipse
• Ant programming
Prerequisite


Last login: Tue Aug 9 13:35:09 on ttys002
brownylins-MacBook:~ brownylin$ ant -version
Apache Ant(TM) version 1.8.2 compiled on June 3 2011
brownylins-MacBook:~ brownylin$ javac -version
javac 1.6.0_26
brownylins-MacBook:~ brownylin$ java -version
java version "1.6.0_26"
Java(TM) SE Runtime Environment (build 1.6.0_26-b03-383-11A511)
Java HotSpot(TM) 64-Bit Server VM (build 20.1-b02-383, mixed mode)
brownylins-MacBook:~ brownylin$
Main.java


package oata;

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World");
    }
}
The Java Way
                   compile and run
md buildclasses
javac -sourcepath src -d buildclasses srcoataHelloWorld.java
java -cp buildclasses oata.HelloWorld




                         package
echo Main-Class: oata.HelloWorld>myManifest
md buildjar
jar cfm buildjarHelloWorld.jar myManifest -C buildclasses .
java -jar buildjarHelloWorld.jar
The Ant Way
A Simple build.xml


<?xml version="1.0"?>
<project name="firstbuild" default="compile" >
   <target name="compile">
      <javac srcdir="." />
      <echo>compilation complete!</echo>
   </target>
</project>
Build


brownylins-MacBook:FirstBuild brownylin$ ant
Buildfile: /Users/brownylin/Desktop/AntPlay/FirstBuild/build.xml

compile:
    [javac] Compiling 1 source file
     [echo] compilation complete!

BUILD SUCCESSFUL
Total time: 0 seconds
-verbose
Run



brownylins-MacBook:FirstBuild brownylin$ java Main a b .
a
b
.
Doing so shows the advantage of placing intermediate code into the build directory:
you can build a JAR file from it without having to list what files are included. This is
because all files in the directory tree should go in the JAR file, which, conveniently, is


                  Structure
the default behavior of the <jar> task.
    With the destination directories defined, we’ve now completed the directory
structure of the project, which looks like the illustration in figure 2.2. When the build




                                            Figure 2.2
                                            The directory layout for our project—
                                            keeping source separate from generated
                                            files. The shaded directories and files are
                                            created during the build.
<?xml version="1.0" ?>
<project name="structured" default="archive" >

   <target name="init">
      <mkdir dir="build/classes" />
      <mkdir dir="dist" />
   </target>

   <target name="compile" depends="init" >
      <javac srcdir="src" destdir="build/classes" />
   </target>

   <target name="archive" depends="compile" >
      <jar destfile="dist/project.jar" basedir="build/
classes" />
   </target>

   <target name="clean" depends="init">
      <delete dir="build" />
      <delete dir="dist" />
   </target>

</project>
Run


brownylins-MacBook:StructureBuild brownylin$ java -cp build/classes
org.antbook.welcome.Main a b .

a
b
.
Practice




• Tutorial: Hello World with Apache Ant
Outline



• A First Ant Build
•Ant and Eclipse
• Ant programming
Eclipse integrated with Ant



• Apache Ant 101: Make Java builds a
  snap

• Make Ant easy with Eclipse
Outline



• A First Ant Build
• Ant and Eclipse
•Ant programming
Introduction

• Encapsulate each activity in the build
  system into a high-level task

<target name="all">
    <mkdir dir="pkg"/>
    <jar basedir="obj" destfile="pkg/prog.jar"/>
    <copy file="index.txt" tofile="pkg/index.txt"/>
</target>
Ant Programming

• Think Ant script as a sequence of build
  tasks

• XML-based format, default naming
  build.xml

• Each Ant’s XML file contains a project
• Each project contains one or more
  targets that represent something the
  user can build
An Example (1/2)
<project name="ant-project" default="all">

    <property name="country" value="New Zealand"/>
    <property name="city" value="Christchurch"/>

    <target name="print-city">
        <echo message="The nicest place in the world is"/>
        <echo message="${city}, ${country}"/>
    </target>

    <target name="print-math">
        <echo message="Two plus two equals four"/>
    </target>

    <target name="all" depends="print-city, print-math">
        <echo message="Thank you!"/>
    </target>

</project>
An Example (2/2)

• Result
     $ ant
     Buildfile: build.xml
     print-city:
          [echo] The nicest place in the world
     is
          [echo] Christchurch, New Zealand
     print-math:
          [echo] Two plus two equals four
     all:
          [echo] Thank you!
     BUILD SUCCESSFUL
     Total time: 218 milliseconds
Using Target (1/4)

• A target is a convenient way to group
  tasks that need to be executed
  sequentially

              ant   compile
              ant   jar
              ant   package
              ant   clean
              ant   javadoc


• The name of an Ant target isn’t
  related to the name of any disk files
Using Target (2/4)

• Internal target
 ‣ Never invoked directly from the command line


<target name="java" depends="init, make-directories">

    ...

</target>
Using Target (3/4)

• Conditional
  <property name="log-enabled" value="1"/>

  <target name="append-to-log" if="log-enabled">
      <echo message="Appending..."/>
  </target>



• Direct execute target
  <target name="java" depends="init, make-dir">
      ...
      <antcall target="check-rules"/>
      ...
  </target>
Using Target (4/4)

• across multiple build files
  <target name="java" depends="init, make-dir">
      ...
      <ant antfile=”utilities.xml” target="check"/>
      ...
  </target>



• <antcall> performance suffers
• <import>
 ‣ Direct insertion
 ‣ Inherit a set of targets and override
Defining Properties (1/3)

• Defined by different ways
 1. As a string
<property name="wife" value="Grace"/>
<property name="dog" value="Stan"/>
<property name="request"
        value="${wife}, please take ${dog} for a walk"/>


 2. As a file system location

<property name="obj-dir" location="obj/i386/debug"/>

${obj-dir} evaluates to
C:UsersPeterworkspaceAnt_Buildspropertiesobji386debug
Defining Properties (2/3)

 • Defined by different ways
    3. Automatically set by the runtime environment
<echo>${os.name}</echo>   // Windows Vista
<echo>${ant.file}</echo>  // C:UsersPeterworkspaceAnt_Builds
                             propertiesbuild.xml
<echo>${user.name}</echo> // Peter


    4. As the result of a <condition> task (is-
       windows set true if system is windows)
<condition property="is-windows">
    <contains string="${os.name}" substring="Windows"/>
</condition>
Defining Properties (3/3)

• Defined by different ways
 5. Defined on the user’s command line (manually
    specifying vs. hard-coding into the build.xml)

        $ ant -Dname=Jones print-name




 6. Loaded from an external properties file
    (externalize a common set of properties)

      <loadproperties srcfile="values.prop"/>
Properties Scope

• Available after defined (top-level or
  inside a target)

• Can be defined only once in a given
  project

• <ant> and <antcall> tasks
 ‣ Enable you to pass property values into the
    newly invoked target (override any previous
    definition only during the execution of that
    target)
Built-In and Optional Tasks

• Basic file operations
• Archiving (.jar, .zip, etc...)
• The compilation of Java code
• The automatic generation of API
  documentation

• Direct access to version-control tools
• Build lifecycle features (build version
  numbers, sending email messages, and
  playing sounds)
uses the B.class file without recompiling it. As a result, nothing that B.java
imports or extends is ever recompiled.

         <javac> and <depend>
   This algorithm works properly in many cases, but it causes incorrect builds in
other cases. (And this is where things get complex.) Imagine a case in which class
A imports class B, which then imports class C (see Figure 7.2). If both A.java
and C.java have been recently modified, the Java compiler is asked to recom-
pile both those files. When compiling class A, the compiler examines B.class
(because B is <depend srcdir="${src}" destdir="${obj}" /> with respect
               imported by A), but because B.class is up-to-date
to B.java, it’s never recompiled. Class Adestdir="${obj}"/>
              <javac srcdir="${src}" therefore uses the existing version of
class B.


                        imports                           imports
         A.java                            B.java                              C.java



recompiles                    examines                              recompiles



         A.class                           B.class                             C.class

Figure 7.2 The <javac> task doesn’t recompile B.java, even though C.java has
changed.
<chomd>, <copy>

• <chmod>
 ‣ Sets the access permissions on a file or
   directory

• <copy>
 ‣ Similar to the Windows copy command and the
   UNIX cp command
<fileset>, <patternset>,
          <condtion>
• Selecting Multiple Files and Directories
    <copy todir="pkg" flatten="true">
        <fileset dir="src">
            <include name="**/*.jpg"/>
            <include name="**/*.png"/>
            <exclude name="**/*flag*"/>
        </fileset>

        <fileset dir="lib">
            <include name="**/*.gif"/>
            <exclude name="**/*flag*"/>
        </fileset>
    </copy>
Extending Ant

•   <exec>: enables you to invoke a shell command

•   <java>: invoke an arbitrary collection of Java
    code by specifying the class path and class
    name

•   <macrodef>: create a new type of task, with the
    definition of that task written in Ant syntax

•   <taskdef>: enables you to implement a task
    using the full power of the Java language

•   <script>: permits code from other scripting
    languages to be directly embedded inside a
    build.xml file (Javascript, Python and Ruby)
Reference
Thanks you :)

More Related Content

What's hot (20)

PPT
Intro to-ant
Manav Prasad
 
PPT
Introduction to Apache Ant
Muhammad Hafiz Hasan
 
PPT
Java build tool_comparison
Manav Prasad
 
PPT
An introduction to maven gradle and sbt
Fabio Fumarola
 
PDF
Mastering Maven 2.0 In 1 Hour V1.3
Matthew McCullough
 
PDF
Maven 3.0 at Øredev
Matthew McCullough
 
PPT
Introduction To Ant
Rajesh Kumar
 
PDF
Ant tutorial
Ratnesh Kumar Singh
 
PPTX
Spring Boot
Jiayun Zhou
 
PPTX
EclipseMAT
Ali Bahu
 
PDF
Django Rest Framework and React and Redux, Oh My!
Eric Palakovich Carr
 
PPT
Apache ant
K. M. Fazle Azim Babu
 
PDF
Django in the Real World
Jacob Kaplan-Moss
 
PPT
Introduction To Ant1
Rajesh Kumar
 
PDF
Connect2017 DEV-1550 Why Java 8? Or, What's a Lambda?
Julian Robichaux
 
PDF
We Are All Testers Now: The Testing Pyramid and Front-End Development
All Things Open
 
ODP
dJango
Bob Chao
 
PDF
Custom deployments with sbt-native-packager
GaryCoady
 
PDF
Play Framework workshop: full stack java web app
Andrew Skiba
 
PDF
Introduction To Django (Strange Loop 2011)
Jacob Kaplan-Moss
 
Intro to-ant
Manav Prasad
 
Introduction to Apache Ant
Muhammad Hafiz Hasan
 
Java build tool_comparison
Manav Prasad
 
An introduction to maven gradle and sbt
Fabio Fumarola
 
Mastering Maven 2.0 In 1 Hour V1.3
Matthew McCullough
 
Maven 3.0 at Øredev
Matthew McCullough
 
Introduction To Ant
Rajesh Kumar
 
Ant tutorial
Ratnesh Kumar Singh
 
Spring Boot
Jiayun Zhou
 
EclipseMAT
Ali Bahu
 
Django Rest Framework and React and Redux, Oh My!
Eric Palakovich Carr
 
Django in the Real World
Jacob Kaplan-Moss
 
Introduction To Ant1
Rajesh Kumar
 
Connect2017 DEV-1550 Why Java 8? Or, What's a Lambda?
Julian Robichaux
 
We Are All Testers Now: The Testing Pyramid and Front-End Development
All Things Open
 
dJango
Bob Chao
 
Custom deployments with sbt-native-packager
GaryCoady
 
Play Framework workshop: full stack java web app
Andrew Skiba
 
Introduction To Django (Strange Loop 2011)
Jacob Kaplan-Moss
 

Viewers also liked (6)

PDF
Apache Ant
teejug
 
PPT
Apache ANT
le.genie.logiciel
 
PPTX
ANT
guestd845f0
 
PPTX
Apache Ant
Ali Bahu
 
PPT
Apache ANT vs Apache Maven
Mudit Gupta
 
PDF
Manen Ant SVN
Sriskandarajah Suhothayan
 
Apache Ant
teejug
 
Apache ANT
le.genie.logiciel
 
Apache Ant
Ali Bahu
 
Apache ANT vs Apache Maven
Mudit Gupta
 
Ad

Similar to Introduction to Apache Ant (20)

PDF
Ant_quick_guide
ducquoc_vn
 
PDF
Build Scripts
Ólafur Andri Ragnarsson
 
PPT
Ant - Another Neat Tool
Kanika2885
 
PPT
Using Ant To Build J2 Ee Applications
Rajesh Kumar
 
PDF
Deploy Flex with Apache Ant
dctrl — studio for creativ technology
 
PDF
Java ant tutorial
Ashoka Vanjare
 
PDF
Any tutor
Yun-Yan Chi
 
PPT
Introduction to Software Build Technology
Philip Johnson
 
ODP
Ant User Guide
Muthuselvam RS
 
PPT
Java Build Tools
­Avishek A
 
PPT
Ant
sundar22in
 
PPTX
Java build tools
Sujit Kumar
 
PDF
Hands On with Maven
Sid Anand
 
PPSX
Build automation for XPages - AUSLUG 2015
gregorbyte
 
PDF
Java, Eclipse, Maven & JSF tutorial
Raghavan Mohan
 
PDF
Java Builds with Maven and Ant
David Noble
 
PDF
Introduction to Maven
Sperasoft
 
PPTX
From Ant to Maven to Gradle a tale of CI tools for JVM
Bucharest Java User Group
 
PPTX
CodeIgniter Ant Scripting
Albert Rosa
 
Ant_quick_guide
ducquoc_vn
 
Ant - Another Neat Tool
Kanika2885
 
Using Ant To Build J2 Ee Applications
Rajesh Kumar
 
Deploy Flex with Apache Ant
dctrl — studio for creativ technology
 
Java ant tutorial
Ashoka Vanjare
 
Any tutor
Yun-Yan Chi
 
Introduction to Software Build Technology
Philip Johnson
 
Ant User Guide
Muthuselvam RS
 
Java Build Tools
­Avishek A
 
Java build tools
Sujit Kumar
 
Hands On with Maven
Sid Anand
 
Build automation for XPages - AUSLUG 2015
gregorbyte
 
Java, Eclipse, Maven & JSF tutorial
Raghavan Mohan
 
Java Builds with Maven and Ant
David Noble
 
Introduction to Maven
Sperasoft
 
From Ant to Maven to Gradle a tale of CI tools for JVM
Bucharest Java User Group
 
CodeIgniter Ant Scripting
Albert Rosa
 
Ad

More from Shih-Hsiang Lin (13)

PDF
Introduction to GNU Make Programming Language
Shih-Hsiang Lin
 
KEY
Ch6 file, saving states, and preferences
Shih-Hsiang Lin
 
PDF
[C++ gui programming with qt4] chap9
Shih-Hsiang Lin
 
PDF
Ch5 intent broadcast receivers adapters and internet
Shih-Hsiang Lin
 
PDF
Ch4 creating user interfaces
Shih-Hsiang Lin
 
PDF
Ch3 creating application and activities
Shih-Hsiang Lin
 
PDF
[C++ GUI Programming with Qt4] chap7
Shih-Hsiang Lin
 
PPTX
[C++ GUI Programming with Qt4] chap4
Shih-Hsiang Lin
 
PPTX
Function pointer
Shih-Hsiang Lin
 
PPT
Introduction to homography
Shih-Hsiang Lin
 
PPTX
Git basic
Shih-Hsiang Lin
 
PPTX
Project Hosting by Google
Shih-Hsiang Lin
 
PDF
An Introduction to Hidden Markov Model
Shih-Hsiang Lin
 
Introduction to GNU Make Programming Language
Shih-Hsiang Lin
 
Ch6 file, saving states, and preferences
Shih-Hsiang Lin
 
[C++ gui programming with qt4] chap9
Shih-Hsiang Lin
 
Ch5 intent broadcast receivers adapters and internet
Shih-Hsiang Lin
 
Ch4 creating user interfaces
Shih-Hsiang Lin
 
Ch3 creating application and activities
Shih-Hsiang Lin
 
[C++ GUI Programming with Qt4] chap7
Shih-Hsiang Lin
 
[C++ GUI Programming with Qt4] chap4
Shih-Hsiang Lin
 
Function pointer
Shih-Hsiang Lin
 
Introduction to homography
Shih-Hsiang Lin
 
Git basic
Shih-Hsiang Lin
 
Project Hosting by Google
Shih-Hsiang Lin
 
An Introduction to Hidden Markov Model
Shih-Hsiang Lin
 

Recently uploaded (20)

PPTX
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PDF
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PDF
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PPTX
Digital Circuits, important subject in CS
contactparinay1
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PDF
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
PPTX
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
PPTX
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
PPT
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Digital Circuits, important subject in CS
contactparinay1
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 

Introduction to Apache Ant

  • 1. Introduction to Ant @brownylin
  • 2. Why • Many IDEs have their own build systems, so why is Ant important to learn and use • Answer: ‣ Eclipse may actually be using Ant ‣ Not just a build tool, must have for automation
  • 3. What is Ant? • Ant Java Make Make Java
  • 4. Outline •A First Ant Build • Ant and Eclipse • Ant programming
  • 5. Prerequisite Last login: Tue Aug 9 13:35:09 on ttys002 brownylins-MacBook:~ brownylin$ ant -version Apache Ant(TM) version 1.8.2 compiled on June 3 2011 brownylins-MacBook:~ brownylin$ javac -version javac 1.6.0_26 brownylins-MacBook:~ brownylin$ java -version java version "1.6.0_26" Java(TM) SE Runtime Environment (build 1.6.0_26-b03-383-11A511) Java HotSpot(TM) 64-Bit Server VM (build 20.1-b02-383, mixed mode) brownylins-MacBook:~ brownylin$
  • 6. Main.java package oata; public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World"); } }
  • 7. The Java Way compile and run md buildclasses javac -sourcepath src -d buildclasses srcoataHelloWorld.java java -cp buildclasses oata.HelloWorld package echo Main-Class: oata.HelloWorld>myManifest md buildjar jar cfm buildjarHelloWorld.jar myManifest -C buildclasses . java -jar buildjarHelloWorld.jar
  • 9. A Simple build.xml <?xml version="1.0"?> <project name="firstbuild" default="compile" > <target name="compile"> <javac srcdir="." /> <echo>compilation complete!</echo> </target> </project>
  • 10. Build brownylins-MacBook:FirstBuild brownylin$ ant Buildfile: /Users/brownylin/Desktop/AntPlay/FirstBuild/build.xml compile: [javac] Compiling 1 source file [echo] compilation complete! BUILD SUCCESSFUL Total time: 0 seconds
  • 13. Doing so shows the advantage of placing intermediate code into the build directory: you can build a JAR file from it without having to list what files are included. This is because all files in the directory tree should go in the JAR file, which, conveniently, is Structure the default behavior of the <jar> task. With the destination directories defined, we’ve now completed the directory structure of the project, which looks like the illustration in figure 2.2. When the build Figure 2.2 The directory layout for our project— keeping source separate from generated files. The shaded directories and files are created during the build.
  • 14. <?xml version="1.0" ?> <project name="structured" default="archive" > <target name="init"> <mkdir dir="build/classes" /> <mkdir dir="dist" /> </target> <target name="compile" depends="init" > <javac srcdir="src" destdir="build/classes" /> </target> <target name="archive" depends="compile" > <jar destfile="dist/project.jar" basedir="build/ classes" /> </target> <target name="clean" depends="init"> <delete dir="build" /> <delete dir="dist" /> </target> </project>
  • 15. Run brownylins-MacBook:StructureBuild brownylin$ java -cp build/classes org.antbook.welcome.Main a b . a b .
  • 16. Practice • Tutorial: Hello World with Apache Ant
  • 17. Outline • A First Ant Build •Ant and Eclipse • Ant programming
  • 18. Eclipse integrated with Ant • Apache Ant 101: Make Java builds a snap • Make Ant easy with Eclipse
  • 19. Outline • A First Ant Build • Ant and Eclipse •Ant programming
  • 20. Introduction • Encapsulate each activity in the build system into a high-level task <target name="all"> <mkdir dir="pkg"/> <jar basedir="obj" destfile="pkg/prog.jar"/> <copy file="index.txt" tofile="pkg/index.txt"/> </target>
  • 21. Ant Programming • Think Ant script as a sequence of build tasks • XML-based format, default naming build.xml • Each Ant’s XML file contains a project • Each project contains one or more targets that represent something the user can build
  • 22. An Example (1/2) <project name="ant-project" default="all"> <property name="country" value="New Zealand"/> <property name="city" value="Christchurch"/> <target name="print-city"> <echo message="The nicest place in the world is"/> <echo message="${city}, ${country}"/> </target> <target name="print-math"> <echo message="Two plus two equals four"/> </target> <target name="all" depends="print-city, print-math"> <echo message="Thank you!"/> </target> </project>
  • 23. An Example (2/2) • Result $ ant Buildfile: build.xml print-city: [echo] The nicest place in the world is [echo] Christchurch, New Zealand print-math: [echo] Two plus two equals four all: [echo] Thank you! BUILD SUCCESSFUL Total time: 218 milliseconds
  • 24. Using Target (1/4) • A target is a convenient way to group tasks that need to be executed sequentially ant compile ant jar ant package ant clean ant javadoc • The name of an Ant target isn’t related to the name of any disk files
  • 25. Using Target (2/4) • Internal target ‣ Never invoked directly from the command line <target name="java" depends="init, make-directories"> ... </target>
  • 26. Using Target (3/4) • Conditional <property name="log-enabled" value="1"/> <target name="append-to-log" if="log-enabled"> <echo message="Appending..."/> </target> • Direct execute target <target name="java" depends="init, make-dir"> ... <antcall target="check-rules"/> ... </target>
  • 27. Using Target (4/4) • across multiple build files <target name="java" depends="init, make-dir"> ... <ant antfile=”utilities.xml” target="check"/> ... </target> • <antcall> performance suffers • <import> ‣ Direct insertion ‣ Inherit a set of targets and override
  • 28. Defining Properties (1/3) • Defined by different ways 1. As a string <property name="wife" value="Grace"/> <property name="dog" value="Stan"/> <property name="request" value="${wife}, please take ${dog} for a walk"/> 2. As a file system location <property name="obj-dir" location="obj/i386/debug"/> ${obj-dir} evaluates to C:UsersPeterworkspaceAnt_Buildspropertiesobji386debug
  • 29. Defining Properties (2/3) • Defined by different ways 3. Automatically set by the runtime environment <echo>${os.name}</echo> // Windows Vista <echo>${ant.file}</echo> // C:UsersPeterworkspaceAnt_Builds propertiesbuild.xml <echo>${user.name}</echo> // Peter 4. As the result of a <condition> task (is- windows set true if system is windows) <condition property="is-windows"> <contains string="${os.name}" substring="Windows"/> </condition>
  • 30. Defining Properties (3/3) • Defined by different ways 5. Defined on the user’s command line (manually specifying vs. hard-coding into the build.xml) $ ant -Dname=Jones print-name 6. Loaded from an external properties file (externalize a common set of properties) <loadproperties srcfile="values.prop"/>
  • 31. Properties Scope • Available after defined (top-level or inside a target) • Can be defined only once in a given project • <ant> and <antcall> tasks ‣ Enable you to pass property values into the newly invoked target (override any previous definition only during the execution of that target)
  • 32. Built-In and Optional Tasks • Basic file operations • Archiving (.jar, .zip, etc...) • The compilation of Java code • The automatic generation of API documentation • Direct access to version-control tools • Build lifecycle features (build version numbers, sending email messages, and playing sounds)
  • 33. uses the B.class file without recompiling it. As a result, nothing that B.java imports or extends is ever recompiled. <javac> and <depend> This algorithm works properly in many cases, but it causes incorrect builds in other cases. (And this is where things get complex.) Imagine a case in which class A imports class B, which then imports class C (see Figure 7.2). If both A.java and C.java have been recently modified, the Java compiler is asked to recom- pile both those files. When compiling class A, the compiler examines B.class (because B is <depend srcdir="${src}" destdir="${obj}" /> with respect imported by A), but because B.class is up-to-date to B.java, it’s never recompiled. Class Adestdir="${obj}"/> <javac srcdir="${src}" therefore uses the existing version of class B. imports imports A.java B.java C.java recompiles examines recompiles A.class B.class C.class Figure 7.2 The <javac> task doesn’t recompile B.java, even though C.java has changed.
  • 34. <chomd>, <copy> • <chmod> ‣ Sets the access permissions on a file or directory • <copy> ‣ Similar to the Windows copy command and the UNIX cp command
  • 35. <fileset>, <patternset>, <condtion> • Selecting Multiple Files and Directories <copy todir="pkg" flatten="true"> <fileset dir="src"> <include name="**/*.jpg"/> <include name="**/*.png"/> <exclude name="**/*flag*"/> </fileset> <fileset dir="lib"> <include name="**/*.gif"/> <exclude name="**/*flag*"/> </fileset> </copy>
  • 36. Extending Ant • <exec>: enables you to invoke a shell command • <java>: invoke an arbitrary collection of Java code by specifying the class path and class name • <macrodef>: create a new type of task, with the definition of that task written in Ant syntax • <taskdef>: enables you to implement a task using the full power of the Java language • <script>: permits code from other scripting languages to be directly embedded inside a build.xml file (Javascript, Python and Ruby)