Apache Ant Hussain Fakhruddin [email_address]
Topics What , Why, Where? Installing Ant build.xml
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
Why Another such tool? Q) Why create another tool when  make, gnumake, jam, nmake  already exist? They all have some limitations They are more Shell Based Limits you to one kind of OS
Why Ant then? Ant is different Ant has Java Class like extensions Use of XML instead of Shell Scripts Each task is taken care by creating its object Standardized Platform Independent
Where to get it You can get Ant from the following links: Apache Ant Homepage: http://ant.apache.org/   (Download > Current Release of Ant > apache-ant<version>-bin.zip 11MB Approx) IDE: Ant comes in as bundled 3 rd  party tool/modules with IDEs like Eclipse, Netbeans, Jdeveloper,IntelliJ IDEA
Ant directory structure
Installing Ant Step 1: Add the  bin  directory  to your path.
Installing Ant Step 2: Set the  ANT_HOME   environment variable to the  directory where you  installed Ant
Installing Ant Step 3: set the  JAVA_HOME  environment variable to your  JDK
build.xml Similar to makefile but an XML file Its the default file where ant will look for what to build, how to build Has projects, targets, tasks
build.xml ..Cont A build.xml file contains the following: Root Tag :  <project> Child Tag:  <target> (at least one) Sub Child Tag: <task>
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)
Output
<project> name:  [Optional] Name of the project basedir: [Optional] The base directory for path calculation.If not set then directory of the build file will be taken as the basedir default: [Required] The default target to be called when no target is specified in command line.  Example: <project name=”hello” default=”first” basedir=”.”> Three attributes of <project>
<target> A <project> may have one or more <target> A <target> defines set of <task> A <target> may depend on other <target> A <target> gets executed only once A <target> has properties which determines how it will be executed 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;>
<task> Independent code which can be executed Can have multiple attributes Ant has some built-in ready to be used tasks Example:  <jar destfile=&quot;hello.jar&quot;> jar is the command, destfile is the parameter 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.
Properties A <project> can have a set of properties They are kind of Variables. Project properties can be set using the <property> tag Example:  <property name= “src”  location= “src” /> <property name= “build”  location= “build” /> <property name= “dist”  location= “dist” />
Properties ..Cont Properties are case sensitive Can be set in an external file Task can use/reference this property by “${prop_name}” Ant supports all system properties. Example:  ${os.name} expands to Operating System name 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
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>
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
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”); } }
Lets write the build.xml First of all , lets examine what you want to do: You'd like to Compile You'd like to package it into a Jar file. You'd also like to clean up the mess. Finally you'd like to run your application Lets create the root tags <project name= “Hello World” default= “compile”> .. </project>
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
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
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
Run the Ant Place your files in the classpath and execute: ant compile ant jar ant run or simply, ant compile jar run
Output
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
Queries?
Thanks for your time About the Author: Hussain Fakhruddin [email_address]

Apache Ant

  • 1.
    Apache Ant HussainFakhruddin [email_address]
  • 2.
    Topics What ,Why, Where? Installing Ant build.xml
  • 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 suchtool? Q) Why create another tool when make, gnumake, jam, nmake already exist? They all have some limitations They are more Shell Based Limits you to one kind of OS
  • 5.
    Why Ant then?Ant is different Ant has Java Class like extensions Use of XML instead of Shell Scripts Each task is taken care by creating its object Standardized Platform Independent
  • 6.
    Where to getit You can get Ant from the following links: Apache Ant Homepage: http://ant.apache.org/ (Download > Current Release of Ant > apache-ant<version>-bin.zip 11MB Approx) IDE: Ant comes in as bundled 3 rd party tool/modules with IDEs like Eclipse, Netbeans, Jdeveloper,IntelliJ IDEA
  • 7.
  • 8.
    Installing Ant Step1: Add the bin directory to your path.
  • 9.
    Installing Ant Step2: Set the ANT_HOME environment variable to the directory where you installed Ant
  • 10.
    Installing Ant Step3: set the JAVA_HOME environment variable to your JDK
  • 11.
    build.xml Similar tomakefile but an XML file Its the default file where ant will look for what to build, how to build Has projects, targets, tasks
  • 12.
    build.xml ..Cont Abuild.xml file contains the following: Root Tag : <project> Child Tag: <target> (at least one) Sub Child Tag: <task>
  • 13.
    Sample build.xml <projectname=&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.
  • 15.
    <project> name: [Optional] Name of the project basedir: [Optional] The base directory for path calculation.If not set then directory of the build file will be taken as the basedir default: [Required] The default target to be called when no target is specified in command line. Example: <project name=”hello” default=”first” basedir=”.”> Three attributes of <project>
  • 16.
    <target> A <project>may have one or more <target> A <target> defines set of <task> A <target> may depend on other <target> A <target> gets executed only once A <target> has properties which determines how it will be executed 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;>
  • 17.
    <task> Independent codewhich can be executed Can have multiple attributes Ant has some built-in ready to be used tasks Example: <jar destfile=&quot;hello.jar&quot;> jar is the command, destfile is the parameter 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.
  • 18.
    Properties A <project>can have a set of properties They are kind of Variables. Project properties can be set using the <property> tag Example: <property name= “src” location= “src” /> <property name= “build” location= “build” /> <property name= “dist” location= “dist” />
  • 19.
    Properties ..Cont Propertiesare case sensitive Can be set in an external file Task can use/reference this property by “${prop_name}” Ant supports all system properties. Example: ${os.name} expands to Operating System name 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
  • 20.
    Sample build.xml withproperties <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 withAnt 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: Writea 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 thebuild.xml First of all , lets examine what you want to do: You'd like to Compile You'd like to package it into a Jar file. You'd also like to clean up the mess. Finally you'd like to run your application Lets create the root tags <project name= “Hello World” default= “compile”> .. </project>
  • 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 AntPlace your files in the classpath and execute: ant compile ant jar ant run or simply, ant compile jar run
  • 28.
  • 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.
  • 31.
    Thanks for yourtime About the Author: Hussain Fakhruddin [email_address]