{"id":197,"date":"2013-07-25T09:33:47","date_gmt":"2013-07-25T09:33:47","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=197"},"modified":"2013-07-29T07:39:33","modified_gmt":"2013-07-29T07:39:33","slug":"apache-ant-tutorial","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/apache-ant-tutorial\/","title":{"rendered":"Apache ANT Tutorial"},"content":{"rendered":"<p>For working with ant you need to first set the path of the environment variables.I am going to do this in command prompt by executing the following line in command prompt as follows<\/p>\n<p><strong>STEP 1<\/strong><br \/>\nI should set the Path for Java Directory and for ant bin directory as above.<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n C:\\Users\\Mugil&gt;set PATH=D:\\Java\\jdk1.6.0_32\\bin;D:\\Mugil\\apache-ant-1.9.0\\bin;\r\n<\/pre>\n<p><strong>STEP 2<\/strong><br \/>\nNow I should create a build.xml file which the ant looks for deployment as follows<br \/>\n<em>build.xml<\/em><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\r\n&lt;project name=&quot;codethataint&quot; default=&quot;default target&quot;&gt;\r\n &lt;target name=&quot;default target&quot;&gt;\r\n  &lt;echo&gt;\r\n   Executing Default Target\r\n  &lt;\/echo&gt;\r\n &lt;\/target&gt;\r\n&lt;\/project&gt;\r\n<\/pre>\n<p><strong>STEP 3<\/strong><br \/>\nTo run the ant file use the following line in command prompt as below<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n d:\\Mugil\\staging&gt;ant\r\n<\/pre>\n<p>In build.xml is a ANT Script that starts with root node project.<br \/>\nProject will have set of Targets<br \/>\nTargets will have set of Tasks<\/p>\n<p><strong>What if I named my build file to some other name other than build.xml say build2.xml<\/strong><br \/>\nIn such case you can use the below command as follows<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n d:\\Mugil\\staging&gt;ant -buildfile \r\n<\/pre>\n<p><strong>What if I want to run a different target<\/strong><\/p>\n<p><em>build2.xml<\/em><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\r\n&lt;project name=&quot;codethataint&quot; default=&quot;default target&quot;&gt;\r\n &lt;target name=&quot;default target&quot;&gt;\r\n  &lt;echo&gt;\r\n   Executing Default Target\r\n  &lt;\/echo&gt;\r\n &lt;\/target&gt;\r\n &lt;target name=&quot;target 2&quot;&gt;\r\n  &lt;echo&gt;\r\n    Target 2 \r\n  &lt;\/echo&gt;\r\n &lt;\/target&gt;\r\n&lt;\/project&gt;\r\n<\/pre>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n d:\\Mugil\\staging&gt;ant -buildfile  &quot;target 2&quot;\r\n<\/pre>\n<p>You can enforce dependency in ANT script by using <strong>depends<\/strong> keyword as in the following script below<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\r\n&lt;project name=&quot;codethataint&quot; default=&quot;target2&quot;&gt;\r\n &lt;target name=&quot;target1&quot;&gt;\r\n  &lt;echo&gt;\r\n   Target 1\r\n  &lt;\/echo&gt;\r\n &lt;\/target&gt;\r\n &lt;target name=&quot;target2&quot; depends=&quot;target1&quot;&gt;\r\n  &lt;echo&gt;\r\n   Target 2\r\n  &lt;\/echo&gt;\r\n &lt;\/target&gt;\r\n&lt;\/project&gt;\r\n<\/pre>\n<p>When you run the ANT script you will see Target 1 and Target 2 displayed on the Screen.Though the default target is target2 since it depends on target1 both are getting displayed.<\/p>\n<p>If there are two targets which depends on each other then circular dependency will occur<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\r\n&lt;project name=&quot;codethataint&quot; default=&quot;target2&quot;&gt;\r\n &lt;target name=&quot;target1&quot; depends=&quot;target2&quot;&gt;\r\n .\r\n .\r\n &lt;target name=&quot;target2&quot; depends=&quot;target1&quot;&gt;\r\n .\r\n .\r\n<\/pre>\n<p><strong>Creating a Folder using build.xml<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\r\n &lt;project name=&quot;ANT1&quot; default=&quot;makedir&quot;&gt; \r\n &lt;property name=&quot;build.dir&quot; location=&quot;D:\\build&quot;\/&gt; \r\n  &lt;target name=&quot;makedir&quot;&gt;\r\n   &lt;mkdir dir=&quot;${build.dir}&quot; \/&gt; \r\n  &lt;\/target&gt;\r\n &lt;\/project&gt;\r\n<\/pre>\n<p>Before you are making a Folder you need to clean it as below<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\r\n&lt;project name=&quot;ANT1&quot; default=&quot;makedir&quot;&gt; \r\n &lt;property name=&quot;build.dir&quot; location=&quot;D:\\build&quot;\/&gt;\r\n &lt;target name=&quot;clean&quot;&gt;\r\n  &lt;delete dir=&quot;${build.dir}&quot;\/&gt;\r\n &lt;\/target&gt; \r\n &lt;target name=&quot;makedir&quot; depends=&quot;clean&quot;&gt;\r\n  &lt;mkdir dir=&quot;${build.dir}&quot; \/&gt; \r\n  &lt;mkdir dir=&quot;${build.dir}\/classes&quot;\/&gt;\r\n &lt;\/target&gt;\r\n&lt;\/project&gt;\r\n<\/pre>\n<p>Below is a Simple java project which uses an ANT script and creates three folder build.The build contains classes and jar folder.The compiled class files will sit in classes folder and jar files will sit in jar folder.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\r\n&lt;project name=&quot;ANT1&quot; default=&quot;makejar&quot; basedir=&quot;.&quot;&gt;\r\n&lt;property name=&quot;src.dir&quot; location=&quot;src&quot;\/&gt;\r\n&lt;property name=&quot;build.dir&quot; location=&quot;D:\\build&quot;\/&gt;\r\n&lt;property name=&quot;project.name&quot; value=&quot;ANT1&quot;\/&gt;\r\n&lt;target name=&quot;clean&quot;&gt;\r\n &lt;delete dir=&quot;${build.dir}&quot;\/&gt;\r\n&lt;\/target&gt; \r\n&lt;target name=&quot;makedir&quot; depends=&quot;clean&quot;&gt;  \r\n &lt;mkdir dir=&quot;${build.dir}&quot; \/&gt;  \r\n &lt;mkdir dir=&quot;${build.dir}\/src\/classes&quot;\/&gt;\r\n&lt;\/target&gt;\r\n&lt;target name=&quot;compile&quot; depends=&quot;makedir&quot;&gt;\r\n  &lt;javac srcdir=&quot;src&quot; destdir=&quot;${build.dir}\/src\/classes&quot; includeantruntime=&quot;false&quot;\/&gt;\r\n &lt;\/target&gt;\r\n&lt;target name=&quot;makejar&quot; depends=&quot;compile&quot;&gt;\r\n  &lt;jar destfile=&quot;${build.dir}\/jars\/${project.name}.jar&quot; basedir=&quot;${build.dir}\/src\/classes&quot; \/&gt;  \r\n&lt;\/target&gt;\r\n&lt;\/project&gt;\r\n<\/pre>\n<p><strong>Project Java File &#8211; Sample.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.tutor;\r\n\r\npublic class Sample \r\n{\r\n  public static void main(String&#x5B;] args) \r\n  {\r\n    System.out.println(&quot;Its working...!&quot;); \r\n  }\r\n}\r\n<\/pre>\n<p>Once you run an ANT script it does the following task as below<br \/>\n1.<strong>Target Clean<\/strong>    &#8211; Deletes the folder build<br \/>\n2.<strong>Target makedir<\/strong>  &#8211; Creates two directory build and Classes<br \/>\n3.<strong>Target compile<\/strong>  &#8211; Compiles code and puts in classes folder as specified in destdir<br \/>\n4.<strong>Target makejar<\/strong>  &#8211; Creates the JAR file as specified in the jar tag<\/p>\n<ul>\n<li>You can see in the above code <strong>src.dir<\/strong> is given location as src which takes the src folder relative to the folder in which the build.xml exists.<\/li>\n<li>In the line above absolute path is used for build.dir as D:\\build<\/li>\n<\/ul>\n<p><strong>Copying folder from one Location to Another<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;target name=&quot;makecopy&quot;&gt;\t\r\n  &lt;copy todir=&quot;D:\/build\/web&quot;&gt;\r\n   &lt;fileset dir=&quot;WebContent&quot;&gt;\r\n   &lt;\/fileset&gt; \r\n  &lt;\/copy&gt;\r\n&lt;\/target&gt;\r\n<\/pre>\n<p>The above code copies the content in from WebContent folder to <strong>D:\/build\/web<\/strong><\/p>\n<p>Simple Copying of File from one directory to another<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;project name=&quot;ANT2&quot; default=&quot;copyTarget&quot; basedir=&quot;.&quot;&gt;\r\n &lt;target name=&quot;copyTarget&quot;&gt;\r\n   &lt;copy file=&quot;WebContent\/index.jsp&quot; tofile=&quot;D:\/build\/mugil\/index2.jsp&quot;\/&gt;\r\n  &lt;\/target&gt;\r\n&lt;\/project&gt;\r\n<\/pre>\n<p>Above I am copying a file index.jsp into location D:\/build\/mugil\/ and saving it as index2.jsp<\/p>\n<p>If you want to copy the file and Save it in to directory use the below code<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n &lt;copy file=&quot;WebContent\/index.jsp&quot; todir=&quot;D:\/build\/mugil\/&quot;\/&gt;\r\n<\/pre>\n<p>If there is no folder then folder will be created before copying file<\/p>\n<p><strong> Excluding certain type of files while copying<\/strong><br \/>\nNow I want to exclude certain types of files from get copied say XML files.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n &lt;target name=&quot;copyTarget&quot;&gt;\r\n  &lt;copy todir=&quot;D:\/build\/web&quot;&gt;\r\n    &lt;fileset dir=&quot;WebContent&quot;&gt;\r\n      &lt;exclude name=&quot;*.xml&quot;\/&gt;\r\n    &lt;\/fileset&gt; \r\n  &lt;\/copy&gt;\r\n &lt;\/target&gt;\r\n<\/pre>\n<p>The above code excludes XML file in current folder from getting copied.Now what if i want the XML files in the sub folder from getting copied<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n &lt;target name=&quot;copyTarget&quot;&gt;\r\n  &lt;copy todir=&quot;D:\/build\/web&quot;&gt;\r\n    &lt;fileset dir=&quot;WebContent&quot;&gt;\r\n      &lt;exclude name=&quot;**\/*.xml&quot;\/&gt;\r\n    &lt;\/fileset&gt; \r\n  &lt;\/copy&gt;\r\n &lt;\/target&gt;\r\n<\/pre>\n<p>** is going to look for files in sub folder and excludes those from getting copied.<\/p>\n<p><strong>How to pack files into Zip<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;zip destfile=&quot;D:\/build\/web\/zip\/Sample.zip&quot;&gt;\r\n &lt;fileset dir=&quot;WebContent&quot;\/&gt;\r\n&lt;\/zip&gt;\r\n<\/pre>\n<p>The above code creates a Zip file with contents in WebContent <\/p>\n<p><strong>Note<\/strong>:<br \/>\nNever hard code version numbers and folder location in build.xml as they are constantly updated over a period of time.You can create a separate file where you can put your version numbers and replace it as properties in build.xml file as shown below<\/p>\n<p><strong>build.properties<\/strong><br \/>\nappversion=1.0<\/p>\n<p><strong>build.xml<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n &lt;project name=&quot;ANT2&quot; default=&quot;copyTarget&quot;&gt;\r\n   &lt;property file=&quot;build.properties&quot;\/&gt;\r\n     &lt;target name=&quot;copyTarget&quot;&gt;\r\n   \t    &lt;echo&gt;${appversion} &lt;\/echo&gt;\r\n     &lt;\/target&gt;\r\n &lt;\/project&gt;\r\n<\/pre>\n<p>OP:1.0<\/p>\n<p>Now I am going to add directory name in build.properties file as below<\/p>\n<p><strong>build.properties<\/strong><br \/>\ndest.dir=D:\\\\NewFiles<\/p>\n<p>The above code creates a folder in D drive with name NewFiles<\/p>\n<p><strong>Why Double Backward slash?<\/strong><br \/>\nIf you are not giving double backward slash then it will take absolute path and creates folder in project work space<\/p>\n<p><strong>Is there any other way I define my folder path?<\/strong><br \/>\nYes.you can define it by absolute path by using forward slash as below.<\/p>\n<p><strong>build.properties<\/strong><br \/>\ndest.dir=D:\/NewFiles<\/p>\n<p><strong>ANT properties are Immutable<\/strong><br \/>\nNow consider the below code.<\/p>\n<p><strong>build.properties<\/strong><br \/>\nappversion=1.1<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;project name=&quot;ANT2&quot; default=&quot;copyTarget&quot;&gt;\r\n  &lt;property file=&quot;build.properties&quot;\/&gt;\r\n  &lt;property name=&quot;appversion&quot; value=&quot;1.0&quot;\/&gt;\r\n  &lt;target name=&quot;copyTarget&quot;&gt;\r\n     &lt;echo&gt;${appversion} &lt;\/echo&gt;\r\n  &lt;\/target&gt;\r\n&lt;\/project&gt;\r\n<\/pre>\n<p>In line 2 I am getting the appversion value set to 1.1. Later I am trying to redefine the same property by setting the value of the attribute to 1.0.But if you print the value using echo statement you can see 1.1 getting printed on console<\/p>\n<p>OP:1.1<\/p>\n<p>Hence ANT Properties are immutable.<\/p>\n<p>Now you want to dynamically make changes in build.properties file on fly.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n &lt;project name=&quot;ANT2&quot; default=&quot;copyTarget&quot;&gt;\r\n   &lt;property   file=&quot;build.properties&quot;\/&gt;\r\n     &lt;propertyfile   file=&quot;build.properties&quot;  comment=&quot;My properties&quot;&gt;\r\n       &lt;entry  key=&quot;appversion&quot; value=&quot;1.3&quot;\/&gt;\r\n     &lt;\/propertyfile&gt;\t\r\n     &lt;target name=&quot;copyTarget&quot;&gt;\r\n       &lt;echo&gt;${appversion} &lt;\/echo&gt;\r\n     &lt;\/target&gt;\r\n &lt;\/project&gt;\r\n<\/pre>\n<p>In build.properties file appversion value is set to 1.1.But by using propertyfile tag i am resetting the value to 1.3.This <strong>Change will also be made in the original build.properties file<\/strong>.So next time when you open build.properties you can note the appversion value got changed to 1.3 and time of change as commented text.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For working with ant you need to first set the path of the environment variables.I am going to do this in command prompt by executing the following line in command prompt as follows STEP 1 I should set the Path for Java Directory and for ant bin directory as above. C:\\Users\\Mugil&gt;set PATH=D:\\Java\\jdk1.6.0_32\\bin;D:\\Mugil\\apache-ant-1.9.0\\bin; STEP 2 Now&hellip; <a href=\"https:\/\/codethataint.com\/blog\/apache-ant-tutorial\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[],"class_list":["post-197","post","type-post","status-publish","format-standard","hentry","category-ant"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/197","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=197"}],"version-history":[{"count":28,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/197\/revisions"}],"predecessor-version":[{"id":200,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/197\/revisions\/200"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=197"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=197"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}