[ Prev ] [ Index ] [ Next ]

ant

Created Saturday 23/2/2008

1. ANT under cygwin

The main ant shell script in $ANT_HOME/bin needs to be modified in order to get ant working under cygwin. The entry for coverting environment variable paths into windows pathnames contains bugs. The entries seem to be in different locations even for the same compiled binaries of ant. So, for ant 1.6.0, the entries immediately follow the following text, which is just before ant is actually executed via java (towards the end of the script)

# For Cygwin, switch paths to Windows format before running java
if $cygwin; then
	  ...

Some versions of the ant script don't convert the CLASSPATH to windows format (in the 'if $cygwin' test). This conversion is required because java, under cygwin, is a windows native application (there's no compiled JVM for cygwin as there is for, e.g., gnu apps like ls(1), cat(1), etc.). A native windows app requires that any pathnames feed into it are also in native format (e.g., dos format with backslashes \ escaped. e.g., c:\\newpath1;c:\\newpath2 and not c:\newpath1;c:\newpath2 as the latter result in c:<NL>ewpath2 where <NL> is a newline character)

The ant script error reads:

if $cygwin; then
  ANT_HOME=`cygpath --windows "$ANT_HOME"`
  JAVA_HOME=`cygpath --windows "$JAVA_HOME"`
  CLASSPATH=`cygpath --path --windows "$CLASSPATH"`
  LOCALCLASSPATH=`cygpath --path --windows "$LOCALCLASSPATH"`
  CYGHOME=`cygpath --windows "$HOME"`
fi

There are 2 errors in the preceeding:

The preceeding code snipet should be changed to something similar to:

if $cygwin; then
  [ -n ANT_HOME] && ANT_HOME=`cygpath --windows "$ANT_HOME"`
  [ -n $JAVA_HOME ] && JAVA_HOME=`cygpath --windows "$JAVA_HOME"`
  [ -n $CLASSPATH ] && CLASSPATH=`cygpath --windows "$CLASSPATH"`
  [ -n $LOCALCLASSPATH ] && LOCALCLASSPATH=`cygpath --windows "$LOCALCLASSPATH"`
  [ -n $CYGHOME ] && CYGHOME=`cygpath --windows "$HOME"`
fi

image Tip: If using perforce, then P4ROOT must be set to get the new factory build process working (especially when using the external-build files). The value of the P4ROOT must be dos format path.

2. Ant contrib 1.0

The ant-contrib package is a 3rd party add-on to ant that has a heap of useful extensions to and such as returning results from <ant> and <antcall> as well as providing mutable variables and procedural support with <if><then> contstructs. All ant-contrib tasks require that the ant contrib jar (e.g., ant-contrib-1.0b3.jar) be in ant's lib path (either default ANT_HOME or specified with -lib option).

2.A. Return values

Returning values from antcalls is achived by a call-by-reference to a property. That is the calling ant passes in a property name that can be set in the called target and then interogated in the calling ant. Antcontrib does this by scoping the properties so that they can change, unlike normal ant properties which are immutable once set).

The return value targets replace the typical targets of <ant> and <antcall> with <antfetch> and <antcallback> respectively. For example: ''' '''

<antcallback target="sometarget" return="returnvalue"/>

The following example demonstrates returning a value from calling a target in another ant file: ''' '''

<antfetch dir="/path1"  target="sometarget" return="returnvalue"/>

Multiple return values are also posibble. To return multiple values provide a comma separated list of variables in the return attribute of the task: ''' '''

<antfetch dir="/path1"  target="sometarget" return="value1, value2"/>

2.B. Arithmetic operations ''' '''

Arithmetic operations like multiply, divide, random number and so on are provided with the ant-contrib library. The <mathtask> task may need to be defined (via <taskdef>). The following example generates a random number between 0 and 100:

<target name="testrand">
    <path id="runtime-path">
        <pathelement location="/path/to/ant-contrib-1.0b1.jar" />
    </path>
<taskdef 
    name="mathtask"
    classname="net.sf.antcontrib.property.MathTask" >
    <classpath refid="runtime-path"/>
</taskdef>
<mathtask result="result">
    <op op="rint">
        <op op="*">
            <num value="100"/>
            <op op="random"/>
        </op>
    </op>
</mathtask>
    </target>

3. Setting JVM options for ant

Many external (non-core) ant tasks can be memory intensive. For example, the junitreport task may run out of memory when executed within the default jvm memory model started by ant. Any jvm option can be passed to the ant jvm by setting hte ANT_OPTS environment variable. E.g., to start ant with a maximum bound of 512 Mb:

bash $ ANT_OPTS=-Xmx512M ant sometarget

Stuart Moorfoot © 23 Feb 2008 foo@bund.com.au


No backlinks to this page.