Apache Ant

9,041 views
8,529 views

Published on

Introduction to Apache Ant

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

No Downloads
Views
Total views
9,041
On SlideShare
0
From Embeds
0
Number of Embeds
50
Actions
Shares
0
Downloads
461
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide

Apache Ant

  1. Apache Ant Hussain Fakhruddin [email_address]
  2. Topics <ul><li>What , Why, Where? </li></ul><ul><li>Installing Ant </li></ul><ul><li>build.xml </li></ul>
  3. What is Ant “ Apache Ant is a Java-based build tool. In theory, it is kind of like Make, but without Make's wrinkles” -Apache Ant Website
  4. Why Another such tool? <ul><li>Q) Why create another tool when make, gnumake, jam, nmake already exist? </li></ul><ul><li>They all have some limitations </li></ul><ul><li>They are more Shell Based </li></ul><ul><li>Limits you to one kind of OS </li></ul>
  5. Why Ant then? <ul><li>Ant is different </li></ul><ul><li>Ant has Java Class like extensions </li></ul><ul><li>Use of XML instead of Shell Scripts </li></ul><ul><li>Each task is taken care by creating its object </li></ul><ul><li>Standardized </li></ul><ul><li>Platform Independent </li></ul>
  6. Where to get it <ul><li>You can get Ant from the following links: </li></ul><ul><li>Apache Ant Homepage: http://ant.apache.org/ (Download > Current Release of Ant > apache-ant<version>-bin.zip 11MB Approx) </li></ul><ul><ul><li>IDE: Ant comes in as bundled 3 rd party tool/modules with IDEs like Eclipse, Netbeans, Jdeveloper,IntelliJ IDEA </li></ul></ul>
  7. Ant directory structure
  8. Installing Ant <ul><li>Step 1: Add the bin directory to your path. </li></ul>
  9. Installing Ant <ul><li>Step 2: Set the ANT_HOME environment variable to the directory where you installed Ant </li></ul>
  10. Installing Ant <ul><li>Step 3: set the JAVA_HOME environment variable to your JDK </li></ul>
  11. build.xml <ul><li>Similar to makefile but an XML file </li></ul><ul><li>Its the default file where ant will look for what to build, how to build </li></ul><ul><li>Has projects, targets, tasks </li></ul>
  12. build.xml ..Cont A build.xml file contains the following: Root Tag : <project> Child Tag: <target> (at least one) Sub Child Tag: <task>
  13. Sample build.xml <project name=&quot;example&quot; default=&quot;hello&quot;> <target name=&quot;hello&quot;> <echo message=&quot;My First Ant&quot; /> </target> </project> Project tag(only one ) Task tag Target Tag (at least one)
  14. Output
  15. <project> <ul><li>name: [Optional] Name of the project </li></ul><ul><li>basedir: [Optional] The base directory for path calculation.If not set then directory of the build file will be taken as the basedir </li></ul><ul><li>default: [Required] The default target to be called when no target is specified in command line. Example: <project name=”hello” default=”first” basedir=”.”> </li></ul>Three attributes of <project>
  16. <target> <ul><li>A <project> may have one or more <target> </li></ul><ul><li>A <target> defines set of <task> </li></ul><ul><li>A <target> may depend on other <target> </li></ul><ul><li>A <target> gets executed only once </li></ul><ul><li>A <target> has properties which determines how it will be executed </li></ul><ul><li>A <target> can be specified when executing ant from command line Example: <target name=&quot;jar&quot; depends=&quot;compile&quot; description=&quot;create a Jar file for the application&quot;> </li></ul>
  17. <task> <ul><li>Independent code which can be executed </li></ul><ul><li>Can have multiple attributes </li></ul><ul><li>Ant has some built-in ready to be used tasks </li></ul><ul><li>Example: <jar destfile=&quot;hello.jar&quot;> </li></ul><ul><li>jar is the command, destfile is the parameter </li></ul><ul><li>This ant task has the same name as the common java command-line utility, JAR, but is really a call to the ant program's built in jar/zip file support. </li></ul>
  18. Properties <ul><li>A <project> can have a set of properties </li></ul><ul><li>They are kind of Variables. </li></ul><ul><li>Project properties can be set using the <property> tag </li></ul><ul><li>Example: <property name= “src” location= “src” /> </li></ul><ul><li><property name= “build” location= “build” /> </li></ul><ul><li><property name= “dist” location= “dist” /> </li></ul>
  19. Properties ..Cont <ul><li>Properties are case sensitive </li></ul><ul><li>Can be set in an external file </li></ul><ul><li>Task can use/reference this property by “${prop_name}” </li></ul><ul><li>Ant supports all system properties. Example: ${os.name} expands to Operating System name </li></ul><ul><li>Ant also has built in properties. Example: ${ant.java.version} expands to JVM version ant detected ${ant.file} expands to absolute path of the build.xml </li></ul>
  20. Sample build.xml with properties <project name=&quot;MyProject&quot; default=&quot;dist&quot; basedir=&quot;.&quot;> <description> simple example build file </description> <!-- set global properties for this build --> <property name=&quot;src&quot; location=&quot;src&quot;/> <property name=&quot;build&quot; location=&quot;build&quot;/> <property name=&quot;dist&quot; location=&quot;dist&quot;/> <target name=&quot;init&quot;> <!-- Create the time stamp --> <tstamp/> <!-- Create the build directory structure used by compile --> <mkdir dir=&quot;${build}&quot;/> </target> <target name=&quot;compile&quot; depends=&quot;init&quot; description=&quot;compile the source &quot; > <!-- Compile the java code from ${src} into ${build} --> <javac srcdir=&quot;${src}&quot; destdir=&quot;${build}&quot;/> </target> <target name=&quot;dist&quot; depends=&quot;compile&quot; description=&quot;generate the distribution&quot; > <!-- Create the distribution directory --> <mkdir dir=&quot;${dist}/lib&quot;/> <!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file --> <jar jarfile=&quot;${dist}/lib/MyProject-${DSTAMP}.jar&quot; basedir=&quot;${build}&quot;/> </target> <target name=&quot;clean&quot; description=&quot;clean up&quot; > <!-- Delete the ${build} and ${dist} directory trees --> <delete dir=&quot;${build}&quot;/> <delete dir=&quot;${dist}&quot;/> </target> </project>
  21. Hello World with Ant Lets compile , pack and run a simple Hello World program in Java. Step 1: Write your Java source code Step 2: Write a build.xml file Step 3: Run Ant with the build.xml
  22. Step 1: Write a simple Hello World in Java public class HelloWorld{ public static void main(String []args) { System.out.println(“Hi, I am from Ant”); } }
  23. Lets write the build.xml <ul><li>First of all , lets examine what you want to do: </li></ul><ul><li>You'd like to Compile </li></ul><ul><li>You'd like to package it into a Jar file. </li></ul><ul><li>You'd also like to clean up the mess. </li></ul><ul><li>Finally you'd like to run your application </li></ul><ul><li>Lets create the root tags </li></ul><ul><li><project name= “Hello World” default= “compile”> </li></ul><ul><li>.. </li></ul><ul><li></project> </li></ul>
  24. build.xml ..cont. Write a project with tasks to compile, package clean and run <project name=&quot;Hello World&quot; default=&quot;compile&quot;> <target name=&quot;compile&quot;> <mkdir dir=&quot;dist&quot; /> <javac srcdir=&quot;.&quot; destdir=&quot;dist&quot; /> </target> </project> Make a directory called “dist” Provide source and destination directories
  25. build.xml ..cont <project name=&quot;Hello World&quot; default=&quot;compile&quot;> <target name=&quot;jar&quot;> <mkdir dir=&quot;dist/jar&quot;/> <jar destfile=&quot;dist/jar/HelloWorld.jar&quot; basedir=&quot;dist&quot;> <manifest> <attribute name=&quot;Main-Class&quot; value=&quot;HelloWorld&quot;/> </manifest> </jar> </target> <target name=&quot;compile&quot;> <mkdir dir=&quot;dist&quot; /> <javac srcdir=&quot;.&quot; destdir=&quot;dist&quot; /> </target> </project> Package Class Name
  26. build.xml ..cont <project name=&quot;Hello World&quot; default=&quot;compile&quot;> <target name=&quot;run&quot;> <java jar=&quot;dist/jar/HelloWorld.jar&quot; fork=&quot;true&quot;/> </target> <target name=&quot;clean&quot;> <delete dir=&quot;dist&quot;/> </target> <target name=&quot;jar&quot;> <mkdir dir=&quot;dist/jar&quot;/> <jar destfile=&quot;dist/jar/HelloWorld.jar&quot; basedir=&quot;dist&quot;> <manifest> <attribute name=&quot;Main-Class&quot; value=&quot;HelloWorld&quot;/> </manifest> </jar> </target> <target name=&quot;compile&quot;> <mkdir dir=&quot;dist&quot; /> <javac srcdir=&quot;.&quot; destdir=&quot;dist&quot; /> </target> </project> delete the directory created by <mkdir> in the beginning Execute this jar file
  27. Run the Ant Place your files in the classpath and execute: ant compile ant jar ant run or simply, ant compile jar run
  28. Output
  29. Ant Resources Homepage: http://ant.apache.org User Manual: http://jakarta.apache.org/ant/manual/index.html Wiki: http://wiki.apache.org/ant/FrontPage FAQ: http://ant.apache.org/faq.html Books: http://sourceforge.net/projects/antbook Contribution: http://www.apache.org/foundation/contributing.html
  30. Queries?
  31. Thanks for your time About the Author: Hussain Fakhruddin [email_address]

×