Show List

Ant Properties

Ant properties are used to store values that can be referenced throughout an Ant build file. Properties are defined using the "property" task, and their values can be set explicitly or by reading from an external file or system environment variable. Here are some examples of how to define and use properties in Ant:

  • Defining a property with an explicit value:
python
Copy code
<property name="app.name" value="MyApp"/>

This sets the "app.name" property to the value "MyApp".

  • Defining a property by reading from an external file:
python
Copy code
<property file="config.properties"/>

This reads the "config.properties" file and creates properties from the key-value pairs in the file.

  • Defining a property by reading from a system environment variable:
python
Copy code
<property environment="env"/> <property name="java.home" value="${env.JAVA_HOME}"/>

This reads the value of the "JAVA_HOME" environment variable and sets the "java.home" property to that value.

  • Using a property in a task:
bash
Copy code
<echo message="The name of the app is ${app.name}"/>

This references the value of the "app.name" property and includes it in the output of the "echo" task.

  • Overriding a property:
python
Copy code
<property name="app.name" value="MyApp" override="true"/>

This overrides the value of the "app.name" property, even if it was previously set elsewhere in the build file or by an external source.

These are just a few examples of how to use Ant properties. Properties are a powerful feature of Ant that can help you create flexible and reusable build scripts.

Ant provides some predefined in-built properties. Users can also create the custom properties.

Ant Predefined properties

Here is the list of some of the predefined properties. Full list can be accessed from the official page https://ant.apache.org/manual/properties.html.

  • basedir - the absolute path of the project's base directory.
  • ant.file - the absolute path of the buildfile.
  • ant.version - the version of Ant
  • ant.project.name - the name of the project that is currently executing. It is set in the name attribute of <project>.
  • ant.java.version - the JVM version Ant detected; currently it can hold the values 9, 1.8, 1.7, 1.6, 1.5, 1.4, 1.3 and 1.2.
  • ant.core.lib - the absolute path of the ant.jar file.
  • ant.home - home directory of Ant
  • ant.library.dir - the directory that has been used to load Ant's jars from. In most cases this is ANT_HOME/lib.

System Properties

Ant also provides access to the system properties. So those can also be accessed in the build tasks if required. Here is the list of some of the system properties.
  • java.version - Java Runtime Environment version
  • java.vendor - Java Runtime Environment vendor
  • java.home - Java installation directory
  • java.vm.name - Java Virtual Machine implementation name
  • java.class.path - Java class path
  • java.library.path - List of paths to search when loading libraries
  • os.name - Operating system name
  • os.arch - Operating system architecture
  • os.version - Operating system version
  • file.separator - File separator ("/" on UNIX)
  • path.separator - Path separator (":" on UNIX)
  • line.separator - Line separator ("\n" on UNIX)
  • user.name - User's account name
  • user.home - User's home directory
  • user.dir - User's current working directory

Separate Property File

User defined properties can also be placed in a separate properties file for better maintenance and reuse. Typically the properties file is named "build.properties" and is placed along side the "build.xml" file. 

Here is the example of build.xml and a separate properties file. Note that in the properties file name and value pairs are seperated by "=" sign.

build.xml 
<?xml version="1.0" ?>
<project name="FirstProject" basedir="." default="run">
    <property file="build.properties"/>

    <target name="clean">
        <!-- Task to delete directory -->
        <delete dir="${build.dir}"/>
    </target>

    <target name="compile" depends="clean">
        <!-- Task to create directory -->
        <mkdir dir="${classes.dir}"/>
        <!-- Task to compile java classes -->
        <javac srcdir="${src.dir}" destdir="${classes.dir}"/>
    </target>

    <target name="jar" depends="compile">
        <!-- Task to create directory -->
        <mkdir dir="${jar.dir}"/>
        <!-- Task to create jar file with Main-Class assignment -->
        <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
            <manifest>
                <attribute name="Main-Class" value="${main-class}"/>
            </manifest>
        </jar>
    </target>

    <target name="run" depends="jar">
        <!-- Task to run the jar. Fork value true enables class execution in another JVM-->
        <java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/>
    </target>

</project>

build.properties:
src.dir=src
build.dir=build
classes.dir=${build.dir}/classes
jar.dir=${build.dir}/jar
main-class=HelloWorld


Next: Ant Macros


    Leave a Comment


  • captcha text