Ant - Another Neat Tool


Published on

Published in: Technology
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Ant - Another Neat Tool

  1. 1. ANT Another Neat Tool
  2. 2. Objective <ul><li>Build Tool </li></ul><ul><ul><li>What is a build tool </li></ul></ul><ul><ul><li>Why do we need a build tool </li></ul></ul><ul><li>ANT </li></ul><ul><ul><li>What is ANT </li></ul></ul><ul><ul><li>Why ANT </li></ul></ul><ul><ul><li>When to use ANT </li></ul></ul><ul><ul><li>How ANT works </li></ul></ul><ul><li>Structure of Build File </li></ul><ul><li>Installing and Running ANT </li></ul><ul><li>More with ANT </li></ul>
  3. 3. What is a build tool <ul><li>A build tool is a programming utility that is used when building a new version of a program. It ensures that source files that have been updated and files that are dependent on them will be compiled into a newer version of the program. </li></ul>
  4. 4. Why do we need build tools <ul><li>Creating a product from source may take several steps: c ompile, link, copy files to various directories, remove intermediate files, generate documentation. </li></ul><ul><li>It becomes problematic to do all these steps manually, first of all because it’s boring, second because it is error-prone. </li></ul><ul><li>The objective should be an automated tool that does all the work for you. Type or click one command and create a final product. </li></ul><ul><li>There are a couple of ways this can be done: </li></ul><ul><ul><li>Write a batch file or script </li></ul></ul><ul><ul><ul><li>The scripts tend to be hard to maintain </li></ul></ul></ul><ul><ul><li>Use a tool designed for the task </li></ul></ul><ul><ul><ul><li>Make </li></ul></ul></ul><ul><ul><ul><li>Ant </li></ul></ul></ul>
  5. 5. What is ANT <ul><li>Java based free build tool from Apache - Build IN Java, USING Java, and FOR Java </li></ul><ul><li>ANT uses XML based configuration “Build” file to drive its actions. </li></ul><ul><li>It is easy to learn and to use, to create powerful Ant build files to compile and bundle applications in .jar, .ear, or .war files, and to deploy J2EE software applications </li></ul>
  6. 6. Why ANT <ul><li>Written in Java so it’s a Cross-Platform build tool, unlike MAKE which is UNIX based. </li></ul><ul><ul><li>Cross platform build files support developers working on different operating systems </li></ul></ul><ul><li>It is extended using Java Classes , unlike other models where it is extended using shell commands. This makes it easily extensible. </li></ul><ul><li>Instead of writing shell commands, the configuration files are XML based which are easy to read and modify. </li></ul>
  7. 7. Why ANT Contd… <ul><li>Faster since each command is executed from within JVM. </li></ul><ul><ul><li>Each command is a thread unlike shell based buildtools where each command is a process. </li></ul></ul><ul><li>One-time setup hassle provides ea sy building of a project </li></ul><ul><li>Ant's Debug Options are very helpful </li></ul>
  8. 8. When should one use ANT <ul><li>On any project with more than a few source files </li></ul><ul><li>Every day for every build… </li></ul>
  9. 9. ANT Directory Structure (Recommended)
  10. 10. How ANT works <ul><li>Each build file has exactly one project. </li></ul><ul><li>Each Build File is made up of at least one target. </li></ul><ul><ul><li>Examples are: 'compile', ‘build', 'clean', etc. </li></ul></ul><ul><li>Each Target is made up of Tasks </li></ul><ul><ul><li>which are executed in a sequence </li></ul></ul><ul><li>Targets can have Dependencies </li></ul><ul><ul><li>Examples: 'install' depends on 'compile' </li></ul></ul><ul><ul><li>Can handle cascading dependencies </li></ul></ul><ul><ul><li>Each Dependency is handled only once </li></ul></ul>
  11. 11. Structure of BUILD File <ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li>< project name=&quot;Test&quot; default=&quot;jar&quot; basedir=&quot;.&quot;> </li></ul><ul><li>< target name=&quot;init&quot;> </li></ul><ul><li>< property file=””/> </li></ul><ul><li><path id=&quot;classpath&quot;> </li></ul><ul><li><pathelement path=&quot;${weblogic.jar}&quot;/> </li></ul><ul><li></path> </li></ul><ul><li>< /target > </li></ul><ul><li><target name=&quot;prepare&quot; depends =&quot;init&quot;> </li></ul><ul><li>< mkdir dir=&quot;${build.dir}/META-INF&quot;/> </li></ul><ul><li></target> </li></ul>
  12. 12. Structure of BUILD File Contd... <ul><li><target name=&quot;clean&quot; depends=&quot;init&quot;> </li></ul><ul><li>< delete dir=&quot;${build.dir}&quot;/> </li></ul><ul><li></target> </li></ul><ul><li><target name=&quot;compile&quot; depends =&quot;prepare&quot;> </li></ul><ul><li>< javac srcdir=&quot;${src.dir}&quot; destdir=&quot;${build.dir} classpathref=&quot;classpath&quot;/> </li></ul><ul><li></target> </li></ul><ul><li><target name=&quot;jar&quot; depends=&quot;compile&quot;> </li></ul><ul><li>< jar destfile=&quot;${dest.dir}/${name}.jar&quot; basedir=&quot;${build.dir}&quot;/> </li></ul><ul><li></target> </li></ul><ul><li></ project > </li></ul>
  13. 13. Project tag <ul><li>First statement should be </li></ul><ul><ul><li><?xml version=&quot;1.0&quot;?> </li></ul></ul><ul><li>Top level XML tag is ‘project’; there should only be one project tag in a build file. </li></ul><ul><li>Project tags specify the basic project attributes and have 3 properties: </li></ul><ul><li> - name : the name of the project. </li></ul><ul><li>- default target : the default target to use when no target is supplied. </li></ul><ul><li>- basedir : the base directory from which all path calculations are done. This attribute might be overridden by setting the &quot;basedir&quot; property beforehand. When this is done, it must be omitted in the project tag. If neither the attribute nor the property have been set, the parent directory of the build file will be used. </li></ul><ul><li>- All three are optional </li></ul><ul><li><project name=&quot;Test&quot; default=&quot;build&quot; basedir=&quot;.&quot;> </li></ul>
  14. 14. Targets <ul><li>A project is made up of a number of targets that tell Ant what it should do like create initial directories, compile source, create javadoc, create jar files, etc. </li></ul><ul><li>Targets consist of a series of tasks that are specific actions for ant to take </li></ul><ul><li>When starting Ant, you can select which target(s) you want to have executed. When no target is given, the project's default is used. </li></ul><ul><li>Targets have these attributes </li></ul><ul><ul><li>name (required) : All targets must have a name such as “compile”, “init”, “clean” </li></ul></ul><ul><ul><li>depends (comma separated list of other targets) : Targets can depend on each other. A target that depends on another target will demand that the other target get executed first </li></ul></ul>
  15. 15. Targets Contd… <ul><ul><li>description : Can have a “description” to explain the target's purpose </li></ul></ul><ul><ul><li>If : “if” will execute a target only if a given property is defined </li></ul></ul><ul><ul><li>Unless : “unless” will only execute a target if a given property is not defined </li></ul></ul><ul><li>Each target is only executed once regardless of the number of other targets that depend on it </li></ul><ul><li>Overall structure of targets: </li></ul><ul><li> <target name=&quot;A&quot;/> </li></ul><ul><li><target name=&quot;B&quot; depends=&quot;A&quot;/> </li></ul><ul><li><target name=&quot;C&quot; depends=&quot;B&quot;/> </li></ul><ul><li><target name=&quot;D&quot; depends=&quot;C,B,A&quot;/> </li></ul><ul><li><target name=&quot;build-module-A&quot; if=&quot;module-A-present&quot;/> </li></ul><ul><li><target name=&quot;build-own-fake-module-A&quot; unless=&quot;module-A-present&quot;/> </li></ul><ul><li>Note : If no if and no unless attribute is present, the target will always be executed. </li></ul>
  16. 16. Useful Target Definitions <ul><li>Init : Sets up properties that will be used throughout the build file. Properties can be set directly or by specifying a properties file. </li></ul><ul><li>Prepare : Create any directory structure which is needed. </li></ul><ul><li>Clean : clean is useful for enabling clean builds. Generally just deletes stuff from previous runs. (ant clean build) </li></ul><ul><li>Compile : Compile some/all of your source files in this target </li></ul><ul><li>Jar : Creates a jar file from the stuff you’ve built </li></ul>
  17. 17. Tasks <ul><li>Each target comprises one or more tasks </li></ul><ul><li>Task is a piece of executable Java code (e.g. javac, jar, etc) </li></ul><ul><li>Tasks do the actual “build” work in Ant </li></ul><ul><li>Ant has core (built in) tasks, optional tasks and the ability to create own tasks </li></ul>
  18. 18. Tasks Contd… <ul><li>Example: </li></ul><ul><li>< name attribute1 =&quot; value1 &quot; attribute2 =&quot; value2 &quot; ... /> </li></ul><ul><li><target name=&quot;prepare&quot; depends=&quot;init“ > </li></ul><ul><li>< mkdir dir=&quot;${build}&quot; /> </li></ul><ul><li></target> </li></ul><ul><li><target name=&quot;build&quot; depends=&quot;copy&quot; > </li></ul><ul><li>< javac srcdir=&quot;src&quot; destdir=&quot;${build}&quot;> </li></ul><ul><li></javac> </li></ul><ul><li></target> </li></ul>
  19. 19. Tasks Contd… <ul><li>Archive Tasks </li></ul><ul><li>Compile Tasks </li></ul><ul><li>Deployment Tasks </li></ul><ul><li>Documentation Tasks </li></ul><ul><li>File Tasks </li></ul><ul><li>Property Tasks </li></ul>
  20. 20. Tasks Contd… <ul><li>javac - The javac task compiles Java source into class files,just as the javac command-line tool does. Ant will recompile only those files that have changed. </li></ul><ul><li>java - Execute a Java class </li></ul><ul><li>javadoc - Generates JavaDoc from your source files </li></ul><ul><li>jar (and war) - Create JAR files </li></ul><ul><li>mkdir - Makes a directory </li></ul><ul><li>copy - Copies files to specified location </li></ul><ul><li>exec - allows different commands to be executed based on the OS it is executing on. </li></ul>
  21. 21. Tasks Contd… <ul><li>delete - Deletes specified files </li></ul><ul><li>parallel - Runs two or more Ant tasks (not targets) simultaneously in separate threads </li></ul><ul><li>Import - Includes another build file into the current file </li></ul><ul><li>echo - Prints a message to the console (default) or a file </li></ul><ul><li>antcall - Calls another target within the same build file </li></ul><ul><li>ant - Calls another target on a different build file </li></ul><ul><li>FTP - lists, gets, puts and deletes files on an FTP server </li></ul><ul><li>Note : You can also write your own tasks. </li></ul>
  22. 22. Properties <ul><li>Property has a name and a value; the name is case-sensitive. Properties may be used in the value of task attributes. This is done by placing the property name between &quot;${&quot; and &quot;}&quot; in the attribute value. </li></ul><ul><li>Example: </li></ul><ul><li><property name=“src” value=“/home/src”/> </li></ul><ul><li><property file=“”/> </li></ul><ul><li>Can use ${src} anywhere in build file to denote /home/src </li></ul><ul><li>Ant provides access to all system properties as if defined by the <property> task </li></ul>
  23. 23. Path Structures <ul><li>Ant provides means to set various environment variables like PATH and CLASSPATH. </li></ul><ul><li>Example of setting CLASSPATH: </li></ul><ul><li><classpath> </li></ul><ul><li><pathelement path=&quot;${classpath}&quot;/> </li></ul><ul><li><pathelement location=&quot;lib/helper.jar&quot;/> </li></ul><ul><li></classpath </li></ul>
  24. 24. Command Line Arguments <ul><li>-buildfile/ -file / -f buildfile – specify build file to use </li></ul><ul><li>targetname – specify target to run (instead of running default) </li></ul><ul><li>-debug – prints debug information </li></ul><ul><li>-logfile filename – log the output in the given file </li></ul><ul><li>-D<property>=<value> - use value for given property </li></ul><ul><li>-help, -h </li></ul><ul><li>-version : print the version information </li></ul>
  25. 25. Installing Ant <ul><li>Download Ant binary distribution from: </li></ul><ul><li> </li></ul><ul><li>Set ANT_HOME to where you installed Ant </li></ul><ul><li>Include $ANT_HOME/bin in PATH </li></ul><ul><li>Make sure JAVA_HOME is set to point to JDK </li></ul><ul><li>Do not ever set CLASSPATH. Ant does not need it, it only causes confusion and breaks things. </li></ul><ul><li>Assume Ant is installed in c:ant. The following sets up the environment: </li></ul><ul><li>set ANT_HOME=c:ant </li></ul><ul><li>set JAVA_HOME=c:j2sdk1.6.0_24 </li></ul><ul><li>set PATH=%PATH%;%ANT_HOME%bin </li></ul><ul><li>Note : To build and use Ant, you must have a JAXP-compliant XML parser installed and available on your classpath, such as Xerces. </li></ul>
  26. 26. Running Ant <ul><li>Type “ant” at the command line </li></ul><ul><li>Automatically looks for build.xml file in current directory to run </li></ul><ul><li>Type “ant –buildfile buildfile.xml ” to specify another build file to run </li></ul><ul><li>ant –help </li></ul><ul><ul><li>lists other command-line options </li></ul></ul>
  27. 27. More with ANT <ul><li><mail> : using this task a mail carrying the build result can be sent to the specified recipients. </li></ul><ul><li>Recording the output : </li></ul><ul><li><record name=&quot;${ant.file}.log“ loglevel=&quot;verbose&quot; /> </li></ul>
  28. 28. References <ul><li> </li></ul><ul><li> </li></ul><ul><li> </li></ul>