Ant

From miki
Jump to navigation Jump to search

ant is the Java based make tool.

Quick Overview

<!-- Display a message -->
<echo message="ANT_HOME is set to = ${env.ANT_HOME}"/>

Cygwin / Portability

Under Cygwin, JDK understands only windows path specification. One can use the following script to convert cygwin paths to windows ones (from [1]):

<property name="some.cygwin.path" value="/cygdrive/h/somepath"/>
<exec executable="cygpath" outputproperty="windows.pathname">
   <arg value="--windows"/>
   <arg value="${some.cygwin.path}"/>
</exec>
<echo message="${windows.pathname}"/>

If the ant script must be portable and must run both under cygwin and linux, use the osfamily property (see [2],[3]) to execute the script above conditionally:

<exec executable="cygpath" outputproperty="some.dir" osfamily="windows">  <!-- convert some.dir if on windows -->
    <arg value="--windows"/>
    <arg value="${linux.dir}"/>
</exec>
<property name="some.dir" value="${linux.dir}"/>    <!-- no effect if osfamily="windows" since properties are immutable -->
<echo message="some.dir is '${some.dir}'"/>

More portability tips:

Another way to detect cygwin ([4]):

<property environment="env"/>
<property name="is${env.OSTYPE}" value="true"/>
<target name="cygCheck" if="iscygwin">
    <echo message="Running under Cygwin"/>
</target>

Properties

Properties are immutable

<project name="demo" default="demo">
    <target name="demo" >
        <property name="aProperty" value="foo" />
        <property name="aProperty" value="bar" /> <!-- already defined; no effect -->
        <echo message="Property value is '${aProperty}'" /> <!-- Displays 'foo' -->
    </target>
</project>

As a result, if you want to define a property only under some condition, and use a default value otherwise, you have to set the property after testing the condition. For instance, this is a test to convert a path under cygwin:

<exec executable="cygpath" outputproperty="some.dir" osfamily="windows">  <!-- convert some.dir if on windows -->
    <arg value="--windows"/>
    <arg value="${linux.dir}"/>
</exec>
<property name="some.dir" value="${linux.dir}"/>    <!-- no effect if osfamily="windows" since properties are immutable -->
<echo message="some.dir is '${some.dir}'"/>

Environment Variables

<property environment="env"/>

provides all environment variables as Ant properties prefixed by "env.". For example, CLASSPATH would be accessible in Ant as ${env.CLASSPATH} (ref [5]).

Define property from environment with default value

Reference: [6]

First solution, using a conditional part:

<property environment="env"/>
<condition property="fileTargetName" value="${env.RELEASE_VER}" else="dev">
    <isset property="env.RELEASE_VER" />
</condition>

From the manual, documentation on the <condition> tag:

Attribute Description Required
property The name of the property to set. Yes
value The value to set the property to. Defaults to "true". No
else The value to set the property to if the condition evaluates to false. By default the property will remain unset. Since Ant 1.6.3 No

Second solution, using the fact that properties are immutable:

<property environment="env"/>
<property name="env.RELEASE_VER" value="dev"/>

Compilation

<javac source="1.4" srcdir="${sources.dir}" destdir="${classes.dir}/" listfiles="true" debug="true" includeAntRuntime="false" includeJavaRuntime="false" >
  <bootclasspath refid="J9Classpath" />
  <classpath refid="compile.extraclasspath" />
</javac>

To add a compilation argument, for instance change encoding to iso-8859-1:

<javac source="1.4" srcdir="${sources.dir}" destdir="${classes.dir}/" listfiles="true" debug="true" includeAntRuntime="false" includeJavaRuntime="false" >
  <compilerarg line="-encoding iso-8859-1"/> 
  <bootclasspath refid="J9Classpath" />
  <classpath refid="compile.extraclasspath" />
</javac>