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 - 3

1)How do I use two different versions of jdk in ant script?

The followings are what I'm doing.

   1. Don't define java.home by yourself. Ant uses an internal one derived from your environment var JAVA_HOME. It is immutable.
   2. I do the followings:
          * In my build.properties (read first)
            jdk13.bin=${tools.home}/jdk1.3.1_13/bin
            jdk14.bin=${tools.home}/j2sdk1.4.2_08/bin/
          * In my master properties file (read last), set default
            javac.location=${jdk13.bin}
          * In my prj.properties, if I need to use 1.4
            javac.location=${jdk14.bin}
          * in my javac task
            executable="${javac.location}/javac.exe"

2)How to pass -Xlint or -Xlint:unchecked to 1.5 javac task?

pass it as compilerarg nested <compilerarg> to specify.

  <compilerarg value="-Xlint"/>
  <!-- or -->
  <compilerarg value="-Xlint:unchecked"/>

3)Can you give me a simple ant xslt task example?

Here is a working one!

    <xslt style="${xslfile}" in="${infile}" out="${outfile}" >
      <classpath>
        <fileset dir="${xml.home}/bin"
          includes="*.jar" />
      </classpath>
    </xslt>

4)How to hide password input?

Override ant Input task.
Response every user input with a backspace. Not the best, but it works

5)How do I add elements to an existing path dynamically?

Yes, it is possible. However, you need to write a custom ant task, get the path, and add/modify it, and put it in use. What I am doing is that I define a path reference lib.classpath, then add/modify the lib.classpath use my own task.

6)How to do conditional statement in ant?

There are many ways to solve the problem.

    * Since target if/unless all depend on some property is defined or not, you can use condition to define different NEW properties, which in turn depends on your ant property values. This makes your ant script very flexible, but a little hard to read.
    * Ant-contrib has <if> <switch> tasks for you to use.
    * Ant-contrib also has <propertyregex> which can make very complicate decisions.

7)Can I change/override ant properties when I use ant-contrib foreach task?

<foreach> is actually using a different property space, you can pass any property name/value pair to it. Just use <param> nested tag inside foreach

8)How to let auto-detect platform and use platform specific properties?

Tell you a great trick, it works excellent.
In your major build-include.xml, put in this line

 
  <property file="${antutil.includes}/${os.name}-${os.arch}.properties" />

This will auto-detect your platform, and you write one file for each environment specific variables. For example: HP-UX-PA_RISC2.0.properties SunOS-sparc.properties Windows XP-x86.properties ... They work great!!!

9)How to make ant user interactive? I tried to use BufferedReader to get user input, it hangs.

See here.
Use this class TimedBufferedReader instead of your BufferedReader will work. This is a working one in our installation process. The original author is credited in the code. I've made some improvement.
TimedBufferedReader.java

package setup;

import java.io.Reader;
import java.io.BufferedReader;
import java.io.IOException;

/**
 * Provides a BufferedReader with a readLine method that
 * blocks for only a specified number of seconds. If no
 * input is read in that time, a specified default
 * string is returned. Otherwise, the input read is returned.
 * Thanks to Stefan Reich
 * for suggesting this implementation.
 * @author: Anthony J. Young-Garner
 * @author: Roseanne Zhang made improvement.
 */

public class TimedBufferedReader extends BufferedReader
{
  private int    timeout    = 60; // 1 minute
  private String defaultStr = "";
            
  /**
   * TimedBufferedReader constructor.
   * @param in Reader
   */
  TimedBufferedReader(Reader in)
  {
    super(in);
  }

  /**
   * TimedBufferedReader constructor.
   * @param in Reader
   * @param sz int Size of the input buffer.
   */
  TimedBufferedReader(Reader in, int sz)
  {
    super(in, sz);
  }

  /**
   * Sets number of seconds to block for input.
   * @param seconds int
   */
  public void setTimeout(int timeout)
  {
    this.timeout=timeout;
  }

  /**
   * Sets defaultStr to use if no input is read.
   * @param str String
   */
  public void setDefaultStr(String str)
  {
    defaultStr = str;
  }
        
  /**
   * We use ms internally
   * @return String
   */
  public String readLine() throws IOException
  {
    int waitms = timeout*1000;
    int ms     = 0;
    while (!this.ready())
    {
      try
      {
        Thread.currentThread().sleep(10);
        ms += 10;
      }
      catch (InterruptedException e)
      {
        break;
      }
      if (ms >= waitms)
      {
          return defaultStr;
      }
    }
    return super.readLine();
  }
}


10)What is a good directory structure for main code and junit test code?

Dev
    |_src
    |  |_com
    |     |_mycom
    |        |_mypkg
    |           |_A.java
    |_test
       |_src
         |_com
            |_mycom
               |_mypkg
                  |_ATest.java
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