Ant: Difference between revisions
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
'''ant''' is the Java based make tool. |
'''ant''' is the Java based make tool. |
||
== Quick Overview == |
|||
<source lang=xml> |
|||
<!-- Display a message --> |
|||
<echo message="ANT_HOME is set to = ${env.ANT_HOME}"/> |
|||
</source> |
|||
== Cygwin / Portability == |
|||
Under Cygwin, JDK understands only windows path specification. One can use the following script to convert cygwin paths to windows ones (from [http://ant.apache.org/manual/platform.html]): |
|||
<source lang=xml> |
|||
<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}"/> |
|||
</source> |
|||
If the ant script must be portable and must run both under cygwin and linux, use the '''osfamily''' property (see [http://stackoverflow.com/questions/12644370/calling-bash-script-from-ant-in-linux-and-cygwin-on-windows],[http://ant-contrib.sourceforge.net/tasks/tasks/osfamily.html]) to execute the script above conditionally: |
|||
<source lang=xml> |
|||
<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}'"/> |
|||
</source> |
|||
More portability tips: |
|||
* http://simonharrer.wordpress.com/tag/osfamily/ |
|||
Another way to detect cygwin ([http://web.archiveorange.com/archive/v/WEoDiJpyauyDlAdtbSbL]): |
|||
<source lang=xml> |
|||
<property environment="env"/> |
|||
<property name="is${env.OSTYPE}" value="true"/> |
|||
<target name="cygCheck" if="iscygwin"> |
|||
<echo message="Running under Cygwin"/> |
|||
</target> |
|||
</source> |
|||
== Properties == |
== Properties == |
||
Line 11: | Line 51: | ||
</target> |
</target> |
||
</project> |
</project> |
||
</source> |
|||
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: |
|||
<source lang=xml> |
|||
<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}'"/> |
|||
</source> |
</source> |
||
Latest revision as of 23:27, 9 January 2013
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>