Apache ant

954 views

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
954
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
56
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Apache ant

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

×