|
|
|
|
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 :
|
|
|
|
|
|
|
|
|
|
|
|
java6 ejb3 jsf hibernate eclipse ajax groovy spring seam java struts webservice j2me guice java5 jca tapestry soa linux ria |
|