Ant conditional branch
I was working on a auto-build file using ant when I wanted to add some conditional branches in the file.
For example, I want to first check whether the environment variables have been set, if yes, then go on build, if not, just break the current build.
The script is like this:
<project name="Test Project" default="build">
<property environment="env" />
<target name="build" depends="path.check">
<echo> continue build </echo>
</target>
<target name="path.check" depends="checkpath" unless="all.variable.set">
<echo>env.JAVA_HOME=${env.JAVA_HOME}</echo>
<echo>env.ECLIPSE_HOME=${env.ECLIPSE_HOME}</echo>
<fail message="not all variables have been set or not set correctly." />
</target>
<target name="checkpath">
<condition property="all.variable.set">
<and>
<available file="${env.JAVA_HOME}" />
<available file="${env.ECLIPSE_HOME}" />
</and>
</condition>
</target>
</project>
The main point of the conditional branch is the "condition" element and the "unless" attribute in the <target>. Please refer to the Apache Ant website for more information.
In this script, we check whether the environment variable "JAVA_HOME" and "ECLIPSE_HOME" have been set, and also check whether the directory exists.
If you only want to test whether this variables have been set, you can change the <condition> block to this:
<condition property="all.variable.set">
<and>
<isset property="env.PROMPT" />
<isset property="env.SONIC_HOME" />
</and>
</condition>
For more details about what you can do in the <condition> element, you can check here.