Listing 1 Condition checking the Ant way
<project default="MAIN">
<tstamp>
<format property="today" pattern="yyyy_MM_dd_HH_mm" />
</tstamp>
<property file="properties/example1.properties" />
<!-- Check to see if file and directory exist -->
<available property="isDir" file="${dir.location}" type="dir" />
<available property="isFile" file="${file.location}" type="file" />
<!-- Test validity of conditions and if found to be true
define the property isConfigured-->
<condition property="isConfigured">
<and>
<isset property="isDir" />
<isset property="isFile" />
<isset property="property.value" />
<not>
<equals arg1="${user.name}" arg2="root" casesensitive="false" />
</not>
</and>
</condition>
<!-- Default target -->
<target name="MAIN">
<!-- Some errors are best failed from the command line such as \
if you cannot log -->
<echo> Trying to open logfile ${log.dir}/example1_${today}_.log
If you fail at this point then the ${log.dir} is probably
not defined correctly in your example1.properties file
</echo>
<record name="${log.dir}/example1_${today}_.log" \
loglevel="error" action="start" />
<antcall target="report_bad_configuration" />
<antcall target="WE_SHOULD_NOT_BE_HERE" />
<record name="${log.dir}/example1_${today}_.log" action="stop" />
</target>
<!--Pump out inbuilt variables -->
<target name="info" description="Show System specific properties">
<echo>
java.version = ${java.version} - Java Runtime Environment version
java.vendor = ${java.vendor} - Java Runtime Environment vendor
java.vendor.url = ${java.vendor.url} - Java vendor URL
java.home = ${java.home} - Java installation directory
java.vm.specification.version = ${java.vm.specification.version} - \
Java Virtual Machine specification version
java.vm.specification.vendor = ${java.vm.specification.vendor} - \
Java Virtual Machine specification vendor
java.vm.specification.name = ${java.vm.specification.name} - \
Java Virtual Machine specification name
java.vm.version = ${java.vm.version} - Java Virtual Machine \
implementation version
java.vm.vendor = ${java.vm.vendor} - Java Virtual Machine \
implementation vendor
java.vm.name = ${java.vm.name} - Java Virtual Machine implementation name
java.specification.version = ${java.specification.version} - \
Java Runtime Environment specification version
java.specification.vendor = ${java.specification.vendor} - \
Java Runtime Environment specification vendor
java.specification.name = ${java.specification.name} - Java \
Runtime Environment specification name
java.class.version = ${java.class.version} - Java class format \
version number
java.class.path = ${java.class.path} - Java class path
java.ext.dirs = ${java.ext.dirs} - Path of extension directory or \
directories
os.name = ${os.name} - Operating system name
os.arch = ${os.arch} - Operating system architecture
os.version = ${os.version} - Operating system version
file.separator = ${file.separator} - File separator ("/" on UNIX)
path.separator = ${path.separator} - Path separator (":" on UNIX)
line.separator = ${line.separator} - Line separator ("\n" on UNIX)
user.name = ${user.name} - User's account name
user.home = ${user.home} - User's home directory
user.dir = ${user.dir} - User's current working directory
</echo>
</target>
<!--If isConfigured is not defined then run this target -->
<target name="report_bad_configuration" unless="isConfigured">
<echo>
System is not configured correctly
Directory exists ${dir.location} = ${isDir}
File exists ${file.location} = ${isFile}
property.value = ${property.value}
Uer run as = ${user.name} - Must not be ROOT
Generic System properties are:
</echo>
<antcall target="info" />
<fail>FAILING GRACEFULLY....</fail>
</target>
<!-- Only run if isConfigured is set -->
<target name="WE_SHOULD_NOT_BE_HERE">
<echo>Ok I will rm -rf from / as root</echo>
</target>
</project>
|