How to Run a Java file from ANT Script

 <java classname="com.mugil.tutor.Sample">
   <classpath path="test/classes/"></classpath>
 </java>

Where Sample points to Sample.class which we got through compilation and test/classes/ contains the Sample.class file. Below is the whole code in build.xml file

<project name="ANT2" default="copyTarget">
  <target name="copyTarget">
  <mkdir dir="test/classes/"/>
  <javac srcdir="src/com/mugil/tutor/" destdir="test/classes/" includeantruntime="false"></javac>
    <java classname="com.mugil.tutor.Sample">
  	<classpath path="test/classes/"></classpath>
     </java>
   </target>
</project>

How to get Current time in ANT Script

 <project name="default" default="target1">
     <target name="target1">		
	<tstamp>
	   <format property="current.time" pattern="yyyyMMdd" />
	   <format property="archive.name" pattern="'MyArchive_'yyyyMMdd_hh:mmaa'.jar'" />
	</tstamp>
	<echo>${current.time}</echo>
	<echo>${archive.name}</echo>		
     </target>
</project>

ANT Script to concatenate two variables and Create directory based on that

<project name="default" default="target1">
  <property name="mugil.dir" value="D:/Mugilvannan/"/>		
     <target name="target1">		
	<tstamp>
	  <format property="current.time" pattern="yyyyMMdd" />
	  <format property="archive.name" pattern="'MyArchive_'yyyyMMdd_hhmmaa" />
	</tstamp>
	<property name="whole.dir" value="${mugil.dir}${archive.name}"/>
	  <echo>
	    ${whole.dir}
	  </echo>		
	<mkdir dir="${whole.dir}"></mkdir>
     </target>
</project>
Posted in ANT.

Error

warning: ‘includeantruntime’ was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds

Reason
It is caused by a misfeature introduced in Ant 1.8

Include attribute includeantruntime and set it to false as shown below

Solution 1

 <javac .... includeantruntime="false" .../>  

Solution 2
In case if your project contains many build.xml files which one cannot, or prefers not to, edit
Use presetdef add these lines in top of build.xml file

 <presetdef name="javac">
    <javac includeantruntime="false" />
  </presetdef>

If your projects do actually need ant runtime libraries, you can either add them explicitly to your build files OR set includeantruntime=”true”

<javac destdir="out" includeantruntime="true">
  <src path="foo.java" />
  <src path="bar.java" />
</javac>

Now all subsequent javac tasks will essentially inherit includeantruntime=”false”

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>set PATH=D:\Java\jdk1.6.0_32\bin;D:\Mugil\apache-ant-1.9.0\bin;

STEP 2
Now I should create a build.xml file which the ant looks for deployment as follows
build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="codethataint" default="default target">
 <target name="default target">
  <echo>
   Executing Default Target
  </echo>
 </target>
</project>

STEP 3
To run the ant file use the following line in command prompt as below

 d:\Mugil\staging>ant

In build.xml is a ANT Script that starts with root node project.
Project will have set of Targets
Targets will have set of Tasks

What if I named my build file to some other name other than build.xml say build2.xml
In such case you can use the below command as follows

 d:\Mugil\staging>ant -buildfile 

What if I want to run a different target

build2.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="codethataint" default="default target">
 <target name="default target">
  <echo>
   Executing Default Target
  </echo>
 </target>
 <target name="target 2">
  <echo>
    Target 2 
  </echo>
 </target>
</project>
 d:\Mugil\staging>ant -buildfile  "target 2"

You can enforce dependency in ANT script by using depends keyword as in the following script below

<?xml version="1.0" encoding="UTF-8"?>
<project name="codethataint" default="target2">
 <target name="target1">
  <echo>
   Target 1
  </echo>
 </target>
 <target name="target2" depends="target1">
  <echo>
   Target 2
  </echo>
 </target>
</project>

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.

If there are two targets which depends on each other then circular dependency will occur

<?xml version="1.0" encoding="UTF-8"?>
<project name="codethataint" default="target2">
 <target name="target1" depends="target2">
 .
 .
 <target name="target2" depends="target1">
 .
 .

Creating a Folder using build.xml

 <?xml version="1.0" encoding="UTF-8"?>
 <project name="ANT1" default="makedir"> 
 <property name="build.dir" location="D:\build"/> 
  <target name="makedir">
   <mkdir dir="${build.dir}" /> 
  </target>
 </project>

Before you are making a Folder you need to clean it as below

<?xml version="1.0" encoding="UTF-8"?>
<project name="ANT1" default="makedir"> 
 <property name="build.dir" location="D:\build"/>
 <target name="clean">
  <delete dir="${build.dir}"/>
 </target> 
 <target name="makedir" depends="clean">
  <mkdir dir="${build.dir}" /> 
  <mkdir dir="${build.dir}/classes"/>
 </target>
</project>

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.

<?xml version="1.0" encoding="UTF-8"?>
<project name="ANT1" default="makejar" basedir=".">
<property name="src.dir" location="src"/>
<property name="build.dir" location="D:\build"/>
<property name="project.name" value="ANT1"/>
<target name="clean">
 <delete dir="${build.dir}"/>
</target> 
<target name="makedir" depends="clean">  
 <mkdir dir="${build.dir}" />  
 <mkdir dir="${build.dir}/src/classes"/>
</target>
<target name="compile" depends="makedir">
  <javac srcdir="src" destdir="${build.dir}/src/classes" includeantruntime="false"/>
 </target>
<target name="makejar" depends="compile">
  <jar destfile="${build.dir}/jars/${project.name}.jar" basedir="${build.dir}/src/classes" />  
</target>
</project>

Project Java File – Sample.java

package com.mugil.tutor;

public class Sample 
{
  public static void main(String[] args) 
  {
    System.out.println("Its working...!"); 
  }
}

Once you run an ANT script it does the following task as below
1.Target Clean – Deletes the folder build
2.Target makedir – Creates two directory build and Classes
3.Target compile – Compiles code and puts in classes folder as specified in destdir
4.Target makejar – Creates the JAR file as specified in the jar tag

  • You can see in the above code src.dir is given location as src which takes the src folder relative to the folder in which the build.xml exists.
  • In the line above absolute path is used for build.dir as D:\build

Copying folder from one Location to Another

<target name="makecopy">	
  <copy todir="D:/build/web">
   <fileset dir="WebContent">
   </fileset> 
  </copy>
</target>

The above code copies the content in from WebContent folder to D:/build/web

Simple Copying of File from one directory to another

<project name="ANT2" default="copyTarget" basedir=".">
 <target name="copyTarget">
   <copy file="WebContent/index.jsp" tofile="D:/build/mugil/index2.jsp"/>
  </target>
</project>

Above I am copying a file index.jsp into location D:/build/mugil/ and saving it as index2.jsp

If you want to copy the file and Save it in to directory use the below code

 <copy file="WebContent/index.jsp" todir="D:/build/mugil/"/>

If there is no folder then folder will be created before copying file

Excluding certain type of files while copying
Now I want to exclude certain types of files from get copied say XML files.

 <target name="copyTarget">
  <copy todir="D:/build/web">
    <fileset dir="WebContent">
      <exclude name="*.xml"/>
    </fileset> 
  </copy>
 </target>

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

 <target name="copyTarget">
  <copy todir="D:/build/web">
    <fileset dir="WebContent">
      <exclude name="**/*.xml"/>
    </fileset> 
  </copy>
 </target>

** is going to look for files in sub folder and excludes those from getting copied.

How to pack files into Zip

<zip destfile="D:/build/web/zip/Sample.zip">
 <fileset dir="WebContent"/>
</zip>

The above code creates a Zip file with contents in WebContent

Note:
Never 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

build.properties
appversion=1.0

build.xml

 <project name="ANT2" default="copyTarget">
   <property file="build.properties"/>
     <target name="copyTarget">
   	    <echo>${appversion} </echo>
     </target>
 </project>

OP:1.0

Now I am going to add directory name in build.properties file as below

build.properties
dest.dir=D:\\NewFiles

The above code creates a folder in D drive with name NewFiles

Why Double Backward slash?
If you are not giving double backward slash then it will take absolute path and creates folder in project work space

Is there any other way I define my folder path?
Yes.you can define it by absolute path by using forward slash as below.

build.properties
dest.dir=D:/NewFiles

ANT properties are Immutable
Now consider the below code.

build.properties
appversion=1.1

<project name="ANT2" default="copyTarget">
  <property file="build.properties"/>
  <property name="appversion" value="1.0"/>
  <target name="copyTarget">
     <echo>${appversion} </echo>
  </target>
</project>

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

OP:1.1

Hence ANT Properties are immutable.

Now you want to dynamically make changes in build.properties file on fly.

 <project name="ANT2" default="copyTarget">
   <property   file="build.properties"/>
     <propertyfile   file="build.properties"  comment="My properties">
       <entry  key="appversion" value="1.3"/>
     </propertyfile>	
     <target name="copyTarget">
       <echo>${appversion} </echo>
     </target>
 </project>

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 Change will also be made in the original build.properties file.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.

Posted in ANT.