Properties Flashcards
What are different ant properties
ant.file The absolute path of the build file.
ant.home The path to executing version of Ant’s root directory.
ant.java.version The JVM version Ant detected; currently it can hold the values 1.1, 1.2,
1.3, and 1.4.
ant.project.name The name of the project that is currently executing; it is set in the name
attribute of <project>.
ant.version The version of Ant.
basedir The absolute path of the project’s basedir (as set with the basedir
attribute of <project>).
<target name=”echo”>
<echo message=”ant.file = ${ant.file}” />
<echo message=”ant.home = ${ant.home}” />
<echo message=”ant.java.version = ${ant.java.version}” />
<echo message=”ant.version = ${ant.version}” />
<echo message=”basedir = ${basedir}” />
<echo message=”user.name = ${user.name}” />
<echo message=”user.home = ${user.home}” />
<echo message=”java.home = ${java.home}” />
</target>
Explain properties
The <property> task allows build files to define their own sets of custom properties.
The most common variants of creating properties are
• Name/value attributes
• Load a set of properties from a properties file
• Load environment variables
<property name=”build.debug” value=”on”/>
<property file=”build.properties”/>
<property environment=”env”/>
Property can apprear outside the task
Properties that are available inside the task:
<available property=”xdoclet.present”
classname=”xdoclet.doc.DocumentDocletTask”
classpath=”${xdoclet.jar}”/>
<echo message=”xdoclet.present = ${xdoclet.present}”/>
<available property=”lib.properties.present”
file=”${lib.dir}/lib.properties”
type=”file”/>
<available property=”resource.exists” resource=”org/example/etc/struts.xml” />
Explain uptodate task
<uptodate property=”tests.unnecessary”>
<srcfiles dir=”src” includes=”**/*.java”/>
<mapper type=”glob” from=”*.java” to=”${build.dir}/classes/*.class” />
</uptodate>
Explain conditions task
Table 3.8 Conditions available within <condition>
Element Definition
<available> Exactly the same semantics and syntax as the <available> task, except
property and value are ignored. Evaluates to true if the resource is available.
<uptodate> Exactly the same semantics and syntax as the <uptodate> task, except property
and value are ignored. Evaluates to true if file(s) are up-to-date.
<os> Evaluates to true if the O/S family (mac, windows, dos, netware, os/2, or
unix), name, architecture, and version match.
<equals> Evaluates to true if both properties have the same value.
<isset> Evaluates to true if the property exists.
<checksum> Uses the same syntax as the <checksum> task, evaluating to true if the
checksum of the file(s) match.
<http> Checks for a status code < 500 from a URL.
<socket> Checks for a socket listener on a specified port and host.
<filesmatch> Byte-for-byte file comparison between two files.
<contains> Tests whether one string contains another, optionally case-sensitive.
<istrue> True if the value is on, true, or yes.
<isfalse> The negation of <istrue>.
<condition property=”tests.unnecessary”>
<and>
<uptodate>
<srcfiles dir=”src” includes=”**/*.java”/>
<mapper type=”glob” from=”*.java” to=”${build.dir}/classes/*.class” />
</uptodate>
<uptodate>
<srcfiles dir=”test” includes=”**/*.java”/>
<mapper type=”glob” from=”*.java” to=”${test.dir}/*.class” />
</uptodate>
<uptodate>
<srcfiles dir=”test” excludes=”**/*.java”/>
<mapper type=”glob” from=”*” to=”${test.dir}/*” />
</uptodate>
</and>
</condition>
How to set properties from command line
<property name=”${lucene.jar}” location=”lib/lucene/lucene.jar”/>
ant -Dlucene.jar=c:/dev/lucene-dev.jar clean dist
ant -propertyfile newlibraries.properties -Dlucene.jar=c:/dev/lucene-dev.jar
Explain timestamp
<tstamp />
DSTAMP “yyyymmdd”
TSTAMP “hhmm”
TODAY “month day year”
<tstamp>
<format property=”dayofweek” pattern=”EEEE”/>
</tstamp>
<echo message=”It is ${dayofweek}”/>
ISO 8601 tstamp
<tstamp>
<format property=”buildtime”
pattern=”yyyy-MM-dd’T’HH:mm:ss” />
</tstamp>
<echo message=”buildtime = ${buildtime}”/>
<tstamp prefix=”start”/>
This sets three properties—start.DSTAMP, start.TSTAMP, and start.TODAY
Explain if/unless conditions
<target name=”copysource” depends=”init” if=”copy.source”>
Some tasks like javac have built in fileset and tags like exclude and include have built in if unless
<javac srcdir=”src”
destdir=”${build.dir}/classes”
<exclude name=”org/example/antbook/xdoclet/*.java”
unless=”xdoclet.present” />
</javac>
Best practices
Use <property location=”…”/> to define file and paths. Use the value
variant for other string values, including file name fragments if needed.
• Nest path definitions. For example, our application has a <path id=”compile.
classpath”> defined, and that same path along with some other
dependencies are needed in our testing compilation and execution. Our <path
id=”test.classpath”> is defined as including <path refid=”compile.
classpath”/>. This is to eliminate duplication and increases maintainability
and reusability.
• Using <filterset> to perform simple text substitutions during a build can
accomplish powerful things like inserting dates or other dynamic build-time
information. Be careful not to use it on binary files, however.
• Take advantage of conditional target execution and conditional patternset capabilities
to allow your build to adapt to its environment. Perhaps it is acceptable
if a dependency is not present, and its absence will simply omit some classes
from compilation, testing, packaging, and deployment. For example, Ant’s very
own build makes extensive use of conditional patternsets to exclude compilation
of the many optional tasks if their dependencies are not present; this allows
Ant to be easily built with no configuration changes or need to install dependencies
for unused tasks.
Carefully consider the directory structure of your project, including how properties
will map to top-level or subordinate directories. By planning this well, a
parent build can easily control where it receives the output of the child build.
View properties that refer to directories as Unix-like mounted directories—they
reside logically as a rooted tree from the base build directory, yet physically do
not necessarily reside in that hierarchy.