View RSS Feed

My Java Tips

ANT properties

Rate this Entry
by , 11-18-2011 at 07:23 PM (1318 Views)
A project in a build.xml can have a set of properties. This property has a name and a value. In which the name of the property is case-sensitive. Properties can be used in the value of task attributes. It is done by putting the property name between "${" and "}" in the attribute value. For example, if there is a "builddir" property with the value "build", then this could be used in an attribute like this: ${builddir}/classes. At run-time this is resolved as build/classes.

Properties are unchangeable. If someone sets a property first, it is freeze for the rest of the build. Properties are also not variable. Properties can be compared with string constants in other languages.

Ant Built-in Properties

Ant provides access to all system properties as if they have been defined using a task. For example, ${} expands to the name of the operating system. This is a list of Ant built-in properties.

basedir - the absolute path of the project's basedir (as set with the basedir attribute of ).
ant.file - the absolute path of the buildfile.
ant.version - the version of Ant - the name of the project that is currently executing; it is set in the name attribute of . - the JVM version Ant detected; For example, it can hold the values "1.2", "1.3", "1.4" and "1.5".

There is also another property. As this property is set by the launcher script, it can not be set inside IDEs:

ant.home- home directory of Ant

Please refer the following build.xml file. Note that the properties are declared out of the target.
Java Code:
<project name="MyProject" default="dist" basedir=".">
        Simple example of build file for properties
<property name="src" location="src"></property>
<property name="build" location="build"></property>
<property name="dist" location="dist"></property>

  <target name="init">
    <mkdir dir="${build}">

  <target name="compile" depends="init">        description="compile the source " &gt;
    <javac srcdir="${src}" destdir="${build}">

  <target name="dist" depends="compile">        description="generate the distribution" &gt;
    <mkdir dir="${dist}/lib">

    <jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar" basedir="${build}">

  <target name="clean">        description="clean up" &gt;
    <delete dir="${build}">
    <delete dir="${dist}">

Via nested
<path> elements a path-like structure can include a reference to another path-like structure:</path></p>

Java Code:
<path id="base.path">
<pathelement path="${classpath}"></pathelement>
      <fileset dir="lib">
        <include name="**/*.jar"></include>
<pathelement location="classes"></pathelement>
<path id="tests.path">
<path refid="base.path"></path>
<pathelement location="testclasses"></pathelement>
The shortcuts mentioned earlier for <classpath> are also valid for
<path>. For example:</path></classpath></p>

Java Code:
<path id="base.path">
<pathelement path="${classpath}"></pathelement>
can also be written as:
<pre lang="java">
<path id="base.path" path="${classpath}"></path>

Submit "ANT properties" to Facebook Submit "ANT properties" to Digg Submit "ANT properties" to Submit "ANT properties" to StumbleUpon Submit "ANT properties" to Google

Updated 11-18-2011 at 07:26 PM by Java Tip