Thursday, October 8, 2009

How is ANT Property Task used?

In order to to set the Ant properties, the <property> task is used. Once property value is set, it cannot be changed. It means they are immutable.In order to set a property to a specific value, the name and value pair is used.

Example

<property name="project.name" value="IQJava"/>

If you want to set a property to a location then name/location assignment is used.
To use the properties surround them with ${}.

Example

<property location="destination" name="dest.dir"/>
<property name="war.dir" location="${dest.dir}/${project.name}"/>

A build.xml as shown in following code box displays how effectively property tasks can be used within a build.xml:
<project basedir="." default="all"> <property name="gensrc.dir" value="gen_src"/> <property name="release.dir" value="release"/> <property name="src.dir" value="../src"/> <property name="lib.dir" value="../externals/lib"/> <property name="class.dir" value="classes"/> <property name="class.dir.lib" value="${class.dir}/lib"/> <property name="class.dir.src" value="${class.dir}/src"/> <import file="one-jar-ant-task.xml"/> <!-- Deletes the directories and files....--> <target name="clean"> <delete dir="${gensrc.dir}"/> <delete dir="${class.dir}"/> <delete> <fileset dir="${lib.dir}" > <include name="**/xyz.jar"/> </fileset> <fileset dir="${release.dir}" > <include name="**/abc.jar"/> </fileset> </delete> </target> <!-- Prepare the Directories needed for code generation --> <target name="prepare"> <mkdir dir="${class.dir}"/> <mkdir dir="${gensrc.dir}"/> <mkdir dir="${class.dir.lib}"/> <mkdir dir="${class.dir.src}"/> </target> <!-- Compile the source code available in src folder --> <target name="compilesrc"> <javac srcdir="${src.dir}" destdir="${class.dir.src}" target="1.4" source="1.4" debug="off" deprecation="on" optimize="off" encoding="iso-8859-1"> <classpath > <fileset dir="${lib.dir}" includes="**/*.jar"/> </classpath> </javac> <copy todir="${class.dir.src}"> <fileset dir="${src.dir}" includes="*.properties"/> </copy> </target> <!-- Generate the lib jar file --> <target name="lib"> <jar manifest="manifest.mf" destfile="${lib.dir}/xyz.jar" includes="**/*.class" basedir="${class.dir.lib}" /> </target> </project>
blog comments powered by Disqus
 

Disclaimer
Interview Questions On Java,Java EE Copyright © 2016. Reads: best tracker