JavaBeat FAQs
JAVABEAT
Home
Articles
Tips
QnA
Forums
FAQs Topics Books Topic
Java Java Books
Struts Struts Books
Spring Spring Books
Hibernate Hibernate Books
JBoss Seam Seam Books
J2EE J2EE Books
EJB EJB Books
JSF JSF Books
Servlets Servlets Books
JDBC JDBC Books
RMI RMI Books
JMS JMS Books
JNI JNI Books
Design Patterns Patterns Books
Exception Exception Books
Apache Ant Ant Books
Groovy
Cobertura
Quartz

Apache Ant Interview Questions

Apache Ant FAQs - 1 | Apache Ant FAQs - 2 | Apache Ant FAQs - 3

Apache Ant Interview Questions - 1


1)What is ant?

Ant is a small animal who can build magnificent buildings. Ant builds!
ANT is a Java based building tool, which is similar to make, and so much better than make.

ANT, what a smart name for a building tool, even the original author of ANT, James Duncan Davidson, meant "Another Neat Tool".

A win-win ant learning method
There is a shortcut.
If you download a small jakarta project, such as Log4J, which is built by ant. It is a good and simple example for you to learn ant. Actually, you hit two birds with one stone.

Ant is easy!

The hard part is how to make a very complicated diversified system work very simple and elegant. Knowledge about ant is not enough, you need an elegant and simple design, you need great naming convention, you need to optimize the code reusability and flexibility, you need a least maintenance system...

Then it is not easy now ..

2)How do I get started to use ant? Can you give me a "Hello World" ant script?

Simple.


    * Download the most recent version of ant from Apache; unzip it some where on your machine.
    * Install j2sdk 1.4 or above.
    * Set JAVA_HOME and ANT_HOME to the directory your installed them respectively.
    * Put %JAVA_HOME%/bin;%ANT_HOME%/bin on your Path. Use ${JAVA_HOME}/bin:${ANT_HOME}/bin on UNIX. Yes, you can use forward slash on windows.
    * Write a "Hello world" build.xml

                  <project name="hello" default="say.hello" basedir="." >
                    <property name="hello.msg" value="Hello, World!" />
                    <target name="say.hello" >
                      <echo>${hello.msg}</echo>
                    </target>
                  </project>
              

    * Type ant in the directory your build.xml located.
    * You are ready to go!!!!

3)How to delete files from a directory if it exists?

The following code fails when directory does not exist!

<delete quiet="true" dir="${classes.dir}" includes="*.class"/>
Your code has many problems.

   1. You should not use implicit fileset, which is deprecated. You should use nested fileset.
   2. If dir does not exist, the build will fail, period!
   3. If you are not sure, use a upper level dir, which exists for sure. See the following fileset.

  <delete>
    <fileset dir="${upperdir.which.exists}">
      <include name="${classes.dir}/*.class" />
    </fileset>
  </delete>
 

4)How do I set classpath in ant?

Here is some snippet of code

  <path id="build.classpath">
    <fileset dir="${build.lib}" includes="**/*.jar"/>
    <fileset dir="${build.classes}" />
  </path>
 
  <target....>
    <javac ....>
      <classpath refid="build.classpath" />
    </java>
  </target>
 
  <target....>
    <java ....>
      <classpath refid="build.classpath" />
    </java>
  </target>
 

5)How does ant read properties? How to set my property system?

Ant sets properties by order, when something is set, the later same properties cannot overwrite the previous ones. This is opposite to your Java setters.
This give us a good leverage of preset all properties in one place, and overwrite only the needed. Give you an example here. You need password for a task, but don't want to share it with your team members, or not the developers outside your team.

Store your password in your ${user.home}/prj.properties
pswd=yourrealpassword
In your include directory master prj.properties
pswd=password
In your build-common.xml read properties files in this order

   1. The commandline will prevail, if you use it: ant -Dpswd=newpassword
   2. ${user.home}/prj.properties (personal)
   3. yourprojectdir/prj.properties (project team wise)
   4. your_master_include_directory/prj.properties (universal)

<cvsnttask password="${pswd} ... />

Problem solved!

6)How to modify properties in ant?

No, you can't!
Properties in Ant are immutable. There is a good reason behind this, see this FAQ item for more details.

7)How to use ant-contrib tasks?

A: Simple, just copy ant-contrib.jar to your ant*/lib directory
And add this line into your ant script, all ant-contrib tasks are now available to you!

    <taskdef resource="net/sf/antcontrib/antcontrib.properties" />


8)How to loop on a list or fileset?

Use ant-contrib <for> <foreach> tasks
General to say, use <for> is better than use <foreach> since for each is actually open another ant property space, use more memory too.

9)Why do I get en exception when I use location="D:\\Code\\include" as attribute of includepath?

See here.
You need to escape the string to "D:\\\\Code\\\\include" or use "D:/Code/include" instead!

Believe me or not? Forward slash works on windows in all ant or java code. It also works in windows environment variables. It does not work in cmd (dos) window before XP. It also works in XP dos window now!

10)Can I put the contents of a classpath or fileset into a property?

Yes, you can.
This is very similar to the call of Java class toString() method and actually it is calling the toString() method inside ant. For example

<fileset id="fs1" dir="t1" includes="**/*.java"/>
<property name="f1.contents" refid="fs1"/>
<echo>f1.contents=${f1.contents}</echo>





Bookmark This Page : | | | | | | | | | | |
Related Articles
Liferay Portal Enterprise Intranets
ZK Developer’s Guide : Online Media Library
The BIRT Environment and Your First Report
AJAX - The Complete Reference
The Java 6.0 Compiler API
Spring Web Flow - Introduction
Introduction to Spring Web Framework
Integrating Struts With Spring
What's new in Struts 2.0? - Struts 2.0 Framework
Introductiion to Jakarta Struts
more articles
java6 ejb3 jsf hibernate eclipse ajax groovy spring seam java struts webservice j2me guice java5 jca tapestry soa linux ria

Sponsors
Webmaster Hosting Forum
Java Jobs
Latest in DLinks
http://javawave.blogspot.com/2008/04/quartz-job-scheduler-part-ii-example.html
Quartz Job Scheduler -- Part 1 (Setting up development project in Netbeans 6.1 beta)
Flex Messaging with BEA Workshop Studio
SOA and Virtualization: How do They Fit Together?
Introduction to Enterprise Portals - Why they Benefit IT and the Business
BEA WebLogic Operations Control: Application Virtualization for Enterprise Java
Best Practices for Building Production Quality EAR Files
BEA's SOA Reference Architecture - A Foundation for Business Agility
Blueprint for Successful SOA Integration
Guardian - What a tool!

JavaBeat Media (2004-2008), India
javabeat home | About Us
our network : opensource softwares
Copyright © 2007 JavaBeat