Java ANT Interview Preparation Guide
Download PDF

Java Ant job interview questions and answers guide. The one who provides the best Java Ant answers with a perfect presentation is the one who wins the interview race. Learn ANT and get preparation for the job of Java Ant

11 ANT Questions and Answers:

1 :: Tell me how to set classpath in ant?

PATH- and CLASSPATH-type references can be specified using both ":" and ";" as separator characters. Ant converts the separator to the correct character of the current operating system.
<classpath>
<pathelement path="${classpath}"/>
<pathelement location="lib/helper.jar"/>
</classpath>
The location attribute specifies a single file or directory relative to the project's base directory (or an absolute filename)
The path attribute accepts colon- or semicolon-separated lists of locations. The path attribute is intended to be used with predefined paths - in any other case; multiple elements with location attributes should be preferred.
The <classpath> tag supports path and location attributes of its own:
<classpath path="${classpath}"/>

2 :: Tell me how to start to use Ant and provide a "Hello World" ant script?

Before start using ant, we should be clear about the project name and the .java files and most importantly, the path where the .class files are to be placed.

For example, we want the application HelloWorld to be used with ant. The Java source files are in a subdirectory called Dirhelloworld, and the .class files are to put into a sub directory called Helloworldclassfiles.

1. The build file by name build.xml is to be written. The script is as follows

<project name=”HelloWorld” default=”compiler” basedir=”.”>
<target name=”compiler”>
<mkdir dir = “Helloworldclassfiles”>
<javac srcdir=”Dirhelloworld” destdir=”Helloworldclassfiles”>
</target>
</project>

2. Now run the ant script to perform the compilation:

C :\> ant
Buildfile: build.xml

and see the results in the extra files and directory created:

c:\>dir Dirhelloworld
c:\>dir Helloworldclassfiles

All the .java files are in Dirhelloworld directory and all the corresponding .class are in Helloworldclassfiles directory.

Note: mkdir command is to be used in MS-DOS and mk dir command is to be used in UNIX/Linux

Dir command is to be used in MS-DOS and ls command is to be used in UNIX /Linux

3 :: Do you know how to modify properties in ant?

Ant properties are immutable; they cannot be changed once initialized. However, it can be done in the following way although it causes the immutable property violation:

<?xml version="1.0"?>
<project name="example" default="run">
<taskdef name="groovy"
classname="org.codehaus.groovy.ant.Groovy"
classpath="lib/groovy-all-1.1-rc-1.jar"/>

<target name="run">
<echo>java.library.path = ${java.library.path}</echo>
<groovy>
properties["java.library.path"] = "changed"
</groovy>
<echo>java.library.path = ${java.library.path}</echo>
</target>
</project>

4 :: Tell me how does ant read properties? How to set my property system?

Properties in ant are set in an order. Once a property is set, later the same property can not overwrite the previous one.

This process provides a good leverage of presetting all properties in one location, and overwriting only the needed properties. For instance, a password is to be used for an application task, and the developer does not want to share the password with his team members or developers of other teams. The following is the process to do the task:

- Store the password in ${user.home}/prj.properties file.
- Set the password – passwd = realpassword of the developer
- Master the prj.properties in include directory
passwd = password
- Read the property files in the following order in the build.xml file
1. Type ant –Dpasswd=newpassword
2. ${user.home}/prj.properties (personal)
3. projectdirectoryname/prj.properties (project team)
4. masterincludedirectory name/prj.properties (universal)

<cvsnttaskpasswd=${password=passwd} … />

5 :: What is the concepts and capabilities of ANT?

Ant is a build tool that is java based. A build tool performs the following tasks:

Compiling java code into byte code
Placing this byte code in a package
Deployment to production systems
Document creation and release notes preparation.

Capabilities of ANT:

ANT tool is extended by using java classes. The configuration files are XML-based. Each task of building directory tree is executed by using the object that implements the Task interface.

ANT provides the cross-platform deployment that can run on any platform.

6 :: Do you know how to make ant user interactive?

The org.apache.tools.ant.input.InputHandler interface is used to implement the user input. To perform the user input, the application creates InputRequest object and this object will be passed to InputHandler. The user input will be rejected if it is invalid.

The InputHandler interface has exactly one method, by name handleInput(InputRequest request). This method throws org.apache.tools.ant.BuildException, if the input is invalid.

7 :: How to write your own ant task?

Define the user defined custom script and use taskdef to define in the custom script.

One can use the tutorial-writing-tasks-src.zip file, which is available in $ANT_HOME/docs/manual directory.

8 :: How to use ant to run a Java application?

The following is an example to run a java application in using ant:

<target name=”run” depends=”some.target”,some.other.target”>
<java classname=”${run.class}” fork=”yes”>
<classpath>
<path refrid = “classpath” />
</classpath>
<jvmarg line=”${debug.jvmargs}”/>
<jvmarg line=”${my.jvmargs}”/> < BR><jvmargvalue=”-Dname=${name}”/>
<jvmarg line=”${run.jvmargs}”/>
<arg line=”${run.args}”/>
</java>
</target>

9 :: Tell me how to use Runtime in ant?

There is no need to use Runtime in ant. Because ant has Runtime counterpart by name ExecTask. ExecTask is in the package org.apache.tools.ant.taskdefs. The Task is created by using the code in the customized ant Task. The code snippet is as follows:

ExecTask execTask = (ExecTask)project.createTask (“exec”);

10 :: Do you know how to debug my ant script?

Ant script can be debugged in the following ways:

By echoing at the place to debug. The problem is easily known. This is similar to printf () function in C and System.out.println() in Java
By using project.log (“message”) in the java script or the customized ant task.
By running Ant with –verbose / -debug options. These options provide more information on what is the process going and at which location