Your SlideShare is downloading. ×
Ant - Another Neat Tool
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Ant - Another Neat Tool

1,429

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,429
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
78
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

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

×