3.7 Checking for the Existence of Properties

3.7.1 Problem

You want to check for the existence of several properties and/or environment variables before you perform a build.

3.7.2 Solution

Define a "checkProperties" target that uses the fail task to abort the build if any properties are undefined.

3.7.3 Discussion

Suppose that various parts of your buildfile require several environment variables. First, specify a target that checks those properties:

<target name="checkProperties">
  <fail unless="env.TOMCAT_HOME">TOMCAT_HOME must be set</fail>
  <fail unless="env.JUNIT_HOME">JUNIT_HOME must be set</fail>
  <fail unless="env.JBOSS_HOME">JBOSS_HOME must be set</fail>

This causes the build to fail if any one of these environment variables is not set. To execute this target, list it as a dependency from some other target:

<target name="compile" depends="checkProperties">

The dependency ensures that the checkProperties target executes before Ant attempts to compile your code.

3.7.4 See Also

The previous two recipes showed how to define environment variables and Ant properties.