Ant: Difference between revisions
Jump to navigation
Jump to search
Line 16: | Line 16: | ||
</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> |
||
Revision as of 23:12, 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}"/>
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 [1]).
Define property from environment with default value
Reference: [2]
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>