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:
<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:
<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:
<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:
<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:
<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 Predefined properties
- 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
- 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
<?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>
src.dir=srcbuild.dir=buildclasses.dir=${build.dir}/classesjar.dir=${build.dir}/jarmain-class=HelloWorld
Leave a Comment