Newsletter:

(FAQ) Ant Interview Questions Page 2

FAQ : Ant Interview Questions Page 2

Page 1 | Page 2

How can I write my own ant task?
Easy!
Writing Your Own Task How-To from ant.
In your own $ANT_HOME/docs/manual directory, there also is tutorial-writing-tasks-src.zip
Use them! Use taskdef to define it in your script, define it before using it.

How to copy files without extention?
If files are in the directory:
<include name="a,b,c"/>
If files are in the directory or subdirectories:
<include name="**/a,**/b,**/c"/>
If you want all files without extension are in the directory or subdirectories:
<exclude name="**/*.*"/>

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"

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"/>

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>

How to hide password input?
Override ant Input task.
Response every user input with a backspace. Not the best, but it works

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.

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.

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

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

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();
}
}

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

Page 1 | Page 2