Ant: Difference between revisions

From miki
Jump to navigation Jump to search
Line 1: Line 1:
'''ant''' is the Java based make tool.
'''ant''' is the Java based make tool.

== Properties ==
=== Properties are immutable ===
<source lang=xml>
<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>
</source>


== Environment Variables ==
== Environment Variables ==
Line 7: Line 19:


provides all environment variables as Ant properties prefixed by "<code>env.</code>". For example, CLASSPATH would be accessible in Ant as <code>${env.CLASSPATH}</code> (ref [http://www.jguru.com/faq/view.jsp?EID=476788]).
provides all environment variables as Ant properties prefixed by "<code>env.</code>". For example, CLASSPATH would be accessible in Ant as <code>${env.CLASSPATH}</code> (ref [http://www.jguru.com/faq/view.jsp?EID=476788]).

=== Define property from environment with default value ===
Reference: [http://stackoverflow.com/questions/936793/define-ant-property-from-environment-with-default-value]

First solution, using a ''conditional'' part:
<source lang=xml>
<property environment="env"/>
<condition property="fileTargetName" value="${env.RELEASE_VER}" else="dev">
<isset property="env.RELEASE_VER" />
</condition>
</source>

From the manual, documentation on the <code>&lt;condition&gt;</code> tag:
{| class=wikitable
|-
!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:
<source lang=xml>
<property environment="env"/>
<property name="env.RELEASE_VER" value="dev"/>
</source>


== Compilation ==
== Compilation ==

Revision as of 12:44, 10 August 2012

ant is the Java based make tool.

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>

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>