SlideShare a Scribd company logo
Apache Ant K. M. Fazle Azim Babu [email_address]
Outline What is Ant Installing Ant Getting Started With Ant Writing Ant build File Running Your First build Anatomy of A build File Sample Completed Build File Running Ant
What is Ant Apache Ant is a Java-based build tool. In theory it's kind of make without make's wrinkles. Instead of writing shell commands, Ant is configured with a build file that's an XML document. The build files are generally platform independent. It's extended using Java classes rather than scripts or shell based commands. Ant can take care of archiving, compilation, execution, documentation, deployment etc.
Installing Ant Ant can be downloaded from http://ant.apache.org/ Ant comes bundled as a zip file or a tarball. Unzip it to some directory.  Set the ANT_HOME environment variable to where you unzipped Ant. Add the ANT_HOME/bin directory to your path. Set the JAVA_HOME environment variable to the location where you installed Java
Getting Started With Ant We'll build with Ant the following Java program Main.java: public class Main {  public static void main(String args[]) { for(int i=0;i<args.length;i++) { System.out.println(args[i]); } } } We want to compile it.
Writing Ant build File Create a file build.xml containing only one target: <?xml version=&quot;1.0&quot;?>  <project name=&quot;firstbuild&quot; default=&quot;compile&quot; > <target name=&quot;compile&quot;> <javac srcdir=&quot;.&quot; /> <echo>compilation complete!</echo> </target> </project> It compiles all Java source code in and below  the current directory  &quot;.&quot;  according to Ant task <javac srcdir=&quot;.&quot; />
Running Your First build Put both Java source file Main.java and build file build.xml in the same, base directory firstbuild Run Ant at the command prompt from this directory: $ ant Buildfile: build.xml compile: [javac] Compiling 1 source file [echo] compilation complete! BUILD SUCCESSFUL Total time: 2 seconds
Running Your First build Ant compiled all Java source  in the current directory (and all its subdirectories, if any) and printed a success <echo> message afterwards.  Now  you can see the resulting Main.class in the directory firstbuild This looks trivial, but Ant can do much more!
Anatomy of A build File Default name for a build file is build.xml Root element must be the ‘project’ element The ‘default’ attribute is required. It Specifies the default target to use. Other attributes available (e.g. name, basedir) Targets contain zero or more tasks The ‘name’ attribute is required Tasks are the smallest units of work Ant Tasks located in ant.jar or ant classpath
Anatomy of A build File.... There is only one ‘project’ per build file One or more 'target' elements ( at least one) Each target can contain as many tasks as are needed May also contain 'properties' like macros in a make file. Properties can be set or included from a file Namespaces supported as of version 1.6 Comments are allowed <!--comment-->
Project  The project tag is used to define the project you wish to work with Projects tags typically contain 3 attributes: name: A logical name for the project default: The default target to execute basedir: The base directory for which all operations are done relative to Additionally, a description for the project can be specified from within the project tag
Project Element Attribute  Description  name  the name of the project.  default *  the default target to use when no target is  supplied. basedir  the base directory from which all path calculations  are done.
Build File Example (Project)  <project name=&quot;Sample Project&quot; default=&quot;compile&quot; basedir=&quot;.&quot;> <description> A sample build file for this project </description> </project>
Target The target tag has the following required attribute : name – the logical name for a target Targets may also have optional attributes such as depends – a list of other target names for which this task is dependant upon, the specified task(s) get executed first description – a description of what a target does Like make files, targets in Ant can depend on some number of other targets For example, we might have a target to create a jarfile, which first depends upon another target to compile the code A build file must specify a default target
Target element Attribute  Description  name *   the name of the target.  depends   a comma-separated list of names of targets on  which this target depends. if   the name of the property that must be set in order  for this target to execute.  unless  name of the property that must not be set in order for this target to execute. description   a short description of this target's function.
Build File Example (Target) <project name=&quot;Sample Project&quot; default=&quot;compile&quot; basedir=&quot;.&quot;> ...  <!-- set up some directories used by this project --> <target name=&quot;init&quot; description=&quot;setup project directories&quot;> </target>  <!-- Compile the java code in src dir into build dir --> <target name=&quot;compile&quot; depends=&quot;init&quot; description=&quot;compile java sources&quot;> </target> <!-- Generate javadocs for current project into docs dir --> <target name=&quot;doc&quot; depends=&quot;init&quot; description=&quot;generate documentation&quot;> </target> <!-- Delete the build & doc directories and Emacs backup (*~) files --> <target name=&quot;clean&quot; description=&quot;tidy up the workspace&quot;> </target> </project>
Task  A task represents an action that needs execution Tasks have a variable number of attributes which are task dependant There are a number of built-in tasks, most of which are things which you would typically do as part of a build process Create a directory Compile java source code Run the javadoc tool over some files Create a jar file from a set of files Remove files/directories And many, many others…
Task element A piece of code that can be executed All have common structure: < name   attribute1 =&quot; value1 “  attribute2 =&quot; value2 &quot; ... />  attributeN is the name of an attribute valueN is the value of attributeN Large set of built-in tasks and optional tasks Complete listing/defs at ant.apache.org/manual Write your own tasks in Java
Properties Build files may contain constants (known as properties) to assign a value to a variable which can then be used throughout the project Makes maintaining large build files more manageable Projects can have a set of properties Property tags consist of a name/value pair Analogous to macros from make Example: <property name=“foo.dist” value=“dist” /> <property file=“foo.properties” />
Build File Example (Properties) <project name=&quot;Sample Project&quot; default=&quot;compile&quot; basedir=&quot;.&quot;> <description> A sample build file for this project </description> <!-- global properties for this build file --> <property name=&quot;source.dir&quot; location=&quot;src&quot;/> <property name=&quot;build.dir&quot; location=&quot;bin&quot;/> <property name=&quot;doc.dir&quot; location=&quot;doc&quot;/>  </project>
Initialization Target & Tasks Our initialization target creates the build and documentation directories The mkdir task creates a directory <project name=&quot;Sample Project&quot; default=&quot;compile&quot; basedir=&quot;.&quot;> ... <!-- set up some directories used by this project --> <target name=&quot;init&quot; description=&quot;setup project directories&quot;> <mkdir dir=&quot;${build.dir}&quot;/> <mkdir dir=&quot;${doc.dir}&quot;/> </target>  ... </project>
Compilation Target & Tasks Our compilation target will compile all java files in the source directory The javac task compiles sources into classes Note the dependence on the init task <project name=&quot;Sample Project&quot; default=&quot;compile&quot; basedir=&quot;.&quot;> ... <!-- Compile the java code in ${src.dir} into ${build.dir} --> <target name=&quot;compile&quot; depends=&quot;init&quot; description=&quot;compile java sources&quot;> <javac srcdir=&quot;${source.dir}&quot; destdir=&quot;${build.dir}&quot;/> </target>  ... </project>
Javadoc Target & Tasks Our documentation target will create the HTML documentation  The javadoc task generates HTML documentation for all sources <project name=&quot;Sample Project&quot; default=&quot;compile&quot; basedir=&quot;.&quot;> ... <!-- Generate javadocs for current project into ${doc.dir} --> <target name=&quot;doc&quot; depends=&quot;init&quot; description=&quot;generate documentation&quot;> <javadoc sourcepath=&quot;${source.dir}&quot; destdir=&quot;${doc.dir}&quot;/> </target> ... </project>
Cleanup Target & Tasks We can also use ant to tidy up our workspace The delete task removes files/directories from the file system <project name=&quot;Sample Project&quot; default=&quot;compile&quot; basedir=&quot;.&quot;> ... <!-- Delete the build & doc directories and Emacs backup (*~) files --> <target name=&quot;clean&quot; description=&quot;tidy up the workspace&quot;> <delete dir=&quot;${build.dir}&quot;/> <delete dir=&quot;${doc.dir}&quot;/> <delete> <fileset defaultexcludes=&quot;no&quot; dir=&quot;${source.dir}&quot; includes=&quot;**/*~&quot;/> </delete> </target> ... </project>
Completed Build File (1 of 3) <project name=&quot;Sample Project&quot; default=&quot;compile&quot; basedir=&quot;.&quot;> <description> A sample build file for this project </description>  <!-- global properties for this build file --> <property name=&quot;source.dir&quot; location=&quot;src&quot;/> <property name=&quot;build.dir&quot; location=&quot;bin&quot;/> <property name=&quot;doc.dir&quot; location=&quot;doc&quot;/>  <!-- set up some directories used by this project -->
Completed Build File (2 of 3) <!-- set up some directories used by this project --> <target name=&quot;init&quot; description=&quot;setup project directories&quot;> <mkdir dir=&quot;${build.dir}&quot;/> <mkdir dir=&quot;${doc.dir}&quot;/> </target> <!-- Compile the java code in ${src.dir} into ${build.dir} --> <target name=&quot;compile&quot; depends=&quot;init&quot; description=&quot;compile java sources&quot;> <javac srcdir=&quot;${source.dir}&quot; destdir=&quot;${build.dir}&quot;/> </target>
Completed Build File (3 of 3) <!-- Generate javadocs for current project into ${doc.dir} --> <target name=&quot;doc&quot; depends=&quot;init&quot; description=&quot;generate documentation&quot;> <javadoc sourcepath=&quot;${source.dir}&quot; destdir=&quot;${doc.dir}&quot;/> </target> <!-- Delete the build & doc directories and Emacs backup (*~) files --> <target name=&quot;clean&quot; description=&quot;tidy up the workspace&quot;> <delete dir=&quot;${build.dir}&quot;/> <delete dir=&quot;${doc.dir}&quot;/> <delete> <fileset defaultexcludes=&quot;no&quot; dir=&quot;${source.dir}&quot; includes=&quot;**/*~&quot;/> </delete> </target> </project>
Running Ant – Command Line Simply cd into the directory with the build.xml file and type ant to run the project default target Or, type ant followed by the name of a target
Running Ant – Eclipse Eclipse comes with out of the box support for Ant No need to separately download and configure Ant Eclipse provides an Ant view Window -> Show View -> Ant Simply drag and drop a build file into the Ant view, then double click the target to run
References http://ant.apache.org/manual/index.html http://wiki.apache.org/ant/FrontPage http://www.vogella.de/articles/ApacheAnt/article.html http://tinyurl.com/3yhkrok http://articles.sitepoint.com/article/apache-ant-demystified http://tinyurl.com/bsa2gv
Thank You For Not Asking :)

More Related Content

PPT
Apache Ant
PPT
Apache Ant
PPT
Ant - Another Neat Tool
PPTX
Apache ant
ODP
Ant User Guide
PPTX
Apache ant
PPT
Introduction To Ant
PDF
Introduction to Apache Ant
Apache Ant
Apache Ant
Ant - Another Neat Tool
Apache ant
Ant User Guide
Apache ant
Introduction To Ant
Introduction to Apache Ant

What's hot (18)

PPT
Apache Ant
PDF
Ant tutorial
PPTX
Apache Ant
PDF
Mastering Maven 2.0 In 1 Hour V1.3
PDF
Building a Dynamic Website Using Django
PPT
Introduction to Apache Ant
PDF
Python & Django TTT
PDF
Maven 3.0 at Øredev
PDF
Best Practices for Front-End Django Developers
PPTX
Django app deployment in Azure By Saurabh Agarwal
PPT
Writing Pluggable Software
PPT
Ruby On Rails Tutorial
PPTX
Django Framework Overview forNon-Python Developers
PPTX
PDF
Deploy Flex with Apache Ant
ODP
Django for Beginners
ODP
Getting started with Django 1.8
PDF
Developing Useful APIs
Apache Ant
Ant tutorial
Apache Ant
Mastering Maven 2.0 In 1 Hour V1.3
Building a Dynamic Website Using Django
Introduction to Apache Ant
Python & Django TTT
Maven 3.0 at Øredev
Best Practices for Front-End Django Developers
Django app deployment in Azure By Saurabh Agarwal
Writing Pluggable Software
Ruby On Rails Tutorial
Django Framework Overview forNon-Python Developers
Deploy Flex with Apache Ant
Django for Beginners
Getting started with Django 1.8
Developing Useful APIs
Ad

Viewers also liked (11)

PPT
Using Ant To Build J2 Ee Applications
PPTX
LatJUG Java Build Tools
PPTX
Maven 2 Introduction
PDF
D Space Installation
PPTX
PPTX
Apache Ant
PPT
Apache ANT vs Apache Maven
PDF
PPS
The Ant Story
Using Ant To Build J2 Ee Applications
LatJUG Java Build Tools
Maven 2 Introduction
D Space Installation
Apache Ant
Apache ANT vs Apache Maven
The Ant Story
Ad

Similar to Apache ant (20)

PPT
Ant - Another Neat Tool
PPT
Introduction To Ant1
PPT
Demystifying Maven
PDF
Ant_quick_guide
PDF
Java ant tutorial
PPT
Intro to-ant
PPT
PPT
Sbt, idea and eclipse
PDF
PPT
Ibm
PPT
Autoconf&Automake
ODP
PPT
Automating API Documentation
PPT
Advanced Rational Robot A Tribute (http://www.geektester.blogspot.com)
PDF
vitepress-en.pdf
PPT
Spring hibernate tutorial
ODP
Phing - A PHP Build Tool (An Introduction)
ODP
Basic Make
PDF
Ext 0523
PDF
Lec 4 06_aug [compatibility mode]
Ant - Another Neat Tool
Introduction To Ant1
Demystifying Maven
Ant_quick_guide
Java ant tutorial
Intro to-ant
Sbt, idea and eclipse
Ibm
Autoconf&Automake
Automating API Documentation
Advanced Rational Robot A Tribute (http://www.geektester.blogspot.com)
vitepress-en.pdf
Spring hibernate tutorial
Phing - A PHP Build Tool (An Introduction)
Basic Make
Ext 0523
Lec 4 06_aug [compatibility mode]

Apache ant

  • 1. Apache Ant K. M. Fazle Azim Babu [email_address]
  • 2. Outline What is Ant Installing Ant Getting Started With Ant Writing Ant build File Running Your First build Anatomy of A build File Sample Completed Build File Running Ant
  • 3. What is Ant Apache Ant is a Java-based build tool. In theory it's kind of make without make's wrinkles. Instead of writing shell commands, Ant is configured with a build file that's an XML document. The build files are generally platform independent. It's extended using Java classes rather than scripts or shell based commands. Ant can take care of archiving, compilation, execution, documentation, deployment etc.
  • 4. Installing Ant Ant can be downloaded from http://ant.apache.org/ Ant comes bundled as a zip file or a tarball. Unzip it to some directory. Set the ANT_HOME environment variable to where you unzipped Ant. Add the ANT_HOME/bin directory to your path. Set the JAVA_HOME environment variable to the location where you installed Java
  • 5. Getting Started With Ant We'll build with Ant the following Java program Main.java: public class Main { public static void main(String args[]) { for(int i=0;i<args.length;i++) { System.out.println(args[i]); } } } We want to compile it.
  • 6. Writing Ant build File Create a file build.xml containing only one target: <?xml version=&quot;1.0&quot;?> <project name=&quot;firstbuild&quot; default=&quot;compile&quot; > <target name=&quot;compile&quot;> <javac srcdir=&quot;.&quot; /> <echo>compilation complete!</echo> </target> </project> It compiles all Java source code in and below the current directory &quot;.&quot; according to Ant task <javac srcdir=&quot;.&quot; />
  • 7. Running Your First build Put both Java source file Main.java and build file build.xml in the same, base directory firstbuild Run Ant at the command prompt from this directory: $ ant Buildfile: build.xml compile: [javac] Compiling 1 source file [echo] compilation complete! BUILD SUCCESSFUL Total time: 2 seconds
  • 8. Running Your First build Ant compiled all Java source in the current directory (and all its subdirectories, if any) and printed a success <echo> message afterwards. Now you can see the resulting Main.class in the directory firstbuild This looks trivial, but Ant can do much more!
  • 9. Anatomy of A build File Default name for a build file is build.xml Root element must be the ‘project’ element The ‘default’ attribute is required. It Specifies the default target to use. Other attributes available (e.g. name, basedir) Targets contain zero or more tasks The ‘name’ attribute is required Tasks are the smallest units of work Ant Tasks located in ant.jar or ant classpath
  • 10. Anatomy of A build File.... There is only one ‘project’ per build file One or more 'target' elements ( at least one) Each target can contain as many tasks as are needed May also contain 'properties' like macros in a make file. Properties can be set or included from a file Namespaces supported as of version 1.6 Comments are allowed <!--comment-->
  • 11. Project The project tag is used to define the project you wish to work with Projects tags typically contain 3 attributes: name: A logical name for the project default: The default target to execute basedir: The base directory for which all operations are done relative to Additionally, a description for the project can be specified from within the project tag
  • 12. Project Element Attribute Description name the name of the project. default * the default target to use when no target is supplied. basedir the base directory from which all path calculations are done.
  • 13. Build File Example (Project) <project name=&quot;Sample Project&quot; default=&quot;compile&quot; basedir=&quot;.&quot;> <description> A sample build file for this project </description> </project>
  • 14. Target The target tag has the following required attribute : name – the logical name for a target Targets may also have optional attributes such as depends – a list of other target names for which this task is dependant upon, the specified task(s) get executed first description – a description of what a target does Like make files, targets in Ant can depend on some number of other targets For example, we might have a target to create a jarfile, which first depends upon another target to compile the code A build file must specify a default target
  • 15. Target element Attribute Description name * the name of the target. depends a comma-separated list of names of targets on which this target depends. if the name of the property that must be set in order for this target to execute. unless name of the property that must not be set in order for this target to execute. description a short description of this target's function.
  • 16. Build File Example (Target) <project name=&quot;Sample Project&quot; default=&quot;compile&quot; basedir=&quot;.&quot;> ... <!-- set up some directories used by this project --> <target name=&quot;init&quot; description=&quot;setup project directories&quot;> </target> <!-- Compile the java code in src dir into build dir --> <target name=&quot;compile&quot; depends=&quot;init&quot; description=&quot;compile java sources&quot;> </target> <!-- Generate javadocs for current project into docs dir --> <target name=&quot;doc&quot; depends=&quot;init&quot; description=&quot;generate documentation&quot;> </target> <!-- Delete the build & doc directories and Emacs backup (*~) files --> <target name=&quot;clean&quot; description=&quot;tidy up the workspace&quot;> </target> </project>
  • 17. Task A task represents an action that needs execution Tasks have a variable number of attributes which are task dependant There are a number of built-in tasks, most of which are things which you would typically do as part of a build process Create a directory Compile java source code Run the javadoc tool over some files Create a jar file from a set of files Remove files/directories And many, many others…
  • 18. Task element A piece of code that can be executed All have common structure: < name attribute1 =&quot; value1 “ attribute2 =&quot; value2 &quot; ... /> attributeN is the name of an attribute valueN is the value of attributeN Large set of built-in tasks and optional tasks Complete listing/defs at ant.apache.org/manual Write your own tasks in Java
  • 19. Properties Build files may contain constants (known as properties) to assign a value to a variable which can then be used throughout the project Makes maintaining large build files more manageable Projects can have a set of properties Property tags consist of a name/value pair Analogous to macros from make Example: <property name=“foo.dist” value=“dist” /> <property file=“foo.properties” />
  • 20. Build File Example (Properties) <project name=&quot;Sample Project&quot; default=&quot;compile&quot; basedir=&quot;.&quot;> <description> A sample build file for this project </description> <!-- global properties for this build file --> <property name=&quot;source.dir&quot; location=&quot;src&quot;/> <property name=&quot;build.dir&quot; location=&quot;bin&quot;/> <property name=&quot;doc.dir&quot; location=&quot;doc&quot;/> </project>
  • 21. Initialization Target & Tasks Our initialization target creates the build and documentation directories The mkdir task creates a directory <project name=&quot;Sample Project&quot; default=&quot;compile&quot; basedir=&quot;.&quot;> ... <!-- set up some directories used by this project --> <target name=&quot;init&quot; description=&quot;setup project directories&quot;> <mkdir dir=&quot;${build.dir}&quot;/> <mkdir dir=&quot;${doc.dir}&quot;/> </target> ... </project>
  • 22. Compilation Target & Tasks Our compilation target will compile all java files in the source directory The javac task compiles sources into classes Note the dependence on the init task <project name=&quot;Sample Project&quot; default=&quot;compile&quot; basedir=&quot;.&quot;> ... <!-- Compile the java code in ${src.dir} into ${build.dir} --> <target name=&quot;compile&quot; depends=&quot;init&quot; description=&quot;compile java sources&quot;> <javac srcdir=&quot;${source.dir}&quot; destdir=&quot;${build.dir}&quot;/> </target> ... </project>
  • 23. Javadoc Target & Tasks Our documentation target will create the HTML documentation The javadoc task generates HTML documentation for all sources <project name=&quot;Sample Project&quot; default=&quot;compile&quot; basedir=&quot;.&quot;> ... <!-- Generate javadocs for current project into ${doc.dir} --> <target name=&quot;doc&quot; depends=&quot;init&quot; description=&quot;generate documentation&quot;> <javadoc sourcepath=&quot;${source.dir}&quot; destdir=&quot;${doc.dir}&quot;/> </target> ... </project>
  • 24. Cleanup Target & Tasks We can also use ant to tidy up our workspace The delete task removes files/directories from the file system <project name=&quot;Sample Project&quot; default=&quot;compile&quot; basedir=&quot;.&quot;> ... <!-- Delete the build & doc directories and Emacs backup (*~) files --> <target name=&quot;clean&quot; description=&quot;tidy up the workspace&quot;> <delete dir=&quot;${build.dir}&quot;/> <delete dir=&quot;${doc.dir}&quot;/> <delete> <fileset defaultexcludes=&quot;no&quot; dir=&quot;${source.dir}&quot; includes=&quot;**/*~&quot;/> </delete> </target> ... </project>
  • 25. Completed Build File (1 of 3) <project name=&quot;Sample Project&quot; default=&quot;compile&quot; basedir=&quot;.&quot;> <description> A sample build file for this project </description> <!-- global properties for this build file --> <property name=&quot;source.dir&quot; location=&quot;src&quot;/> <property name=&quot;build.dir&quot; location=&quot;bin&quot;/> <property name=&quot;doc.dir&quot; location=&quot;doc&quot;/> <!-- set up some directories used by this project -->
  • 26. Completed Build File (2 of 3) <!-- set up some directories used by this project --> <target name=&quot;init&quot; description=&quot;setup project directories&quot;> <mkdir dir=&quot;${build.dir}&quot;/> <mkdir dir=&quot;${doc.dir}&quot;/> </target> <!-- Compile the java code in ${src.dir} into ${build.dir} --> <target name=&quot;compile&quot; depends=&quot;init&quot; description=&quot;compile java sources&quot;> <javac srcdir=&quot;${source.dir}&quot; destdir=&quot;${build.dir}&quot;/> </target>
  • 27. Completed Build File (3 of 3) <!-- Generate javadocs for current project into ${doc.dir} --> <target name=&quot;doc&quot; depends=&quot;init&quot; description=&quot;generate documentation&quot;> <javadoc sourcepath=&quot;${source.dir}&quot; destdir=&quot;${doc.dir}&quot;/> </target> <!-- Delete the build & doc directories and Emacs backup (*~) files --> <target name=&quot;clean&quot; description=&quot;tidy up the workspace&quot;> <delete dir=&quot;${build.dir}&quot;/> <delete dir=&quot;${doc.dir}&quot;/> <delete> <fileset defaultexcludes=&quot;no&quot; dir=&quot;${source.dir}&quot; includes=&quot;**/*~&quot;/> </delete> </target> </project>
  • 28. Running Ant – Command Line Simply cd into the directory with the build.xml file and type ant to run the project default target Or, type ant followed by the name of a target
  • 29. Running Ant – Eclipse Eclipse comes with out of the box support for Ant No need to separately download and configure Ant Eclipse provides an Ant view Window -> Show View -> Ant Simply drag and drop a build file into the Ant view, then double click the target to run
  • 30. References http://ant.apache.org/manual/index.html http://wiki.apache.org/ant/FrontPage http://www.vogella.de/articles/ApacheAnt/article.html http://tinyurl.com/3yhkrok http://articles.sitepoint.com/article/apache-ant-demystified http://tinyurl.com/bsa2gv
  • 31. Thank You For Not Asking :)