You are currently browsing the monthly archive for January 2008.
I can’t say I’m a fan of the Apache Ant build tool but that’s what our project uses so I’ve been trying to wrap my small brain around it enough to make some small changes to our configurations.
Today I needed to call an exec task in a build.xml file on the condition that the executable is in the user’s $PATH. This is what I came up with. Other suggestions are welcome. In my case I needed to execute svn (because the svn
task doesn’t provide an api for the svn info
command) but the general conditional will apply to any executable.
<target name="buildInfo" depends="canDoSvn,svnInfo/>
<target name="canDoSvn">
<property environment="env" />
<condition property="can.do.svn">
<and>
<available file="${projectsDir}/${proj}/.svn" type="dir" />
<available file="svn" type="file">
<filepath>
<pathelement path="${env.PATH}"/>
</filepath>
</available>
</and>
</condition>
</target>
<target name="svnInfo" if="can.do.svn">
<exec dir="${projectsDir}"
executable="svn"
outputproperty="svnInfoOut"
failonerror="false"
failifexecutionfails="false">
<arg line="info ${proj}"/>
</exec>
</target>
To kick things off, the buildInfo
target is called elsewhere in the build script. This target defines dependancies for two other targets that get called in listed order.
In the canDoSvn
target I test if the file svn
can be found in the user’s $PATH. To do this I set the property
task’s environment
attribute to env
which gives me a handle on the user’s PATH
environment variable: ${env.PATH}
. The path
attribute of pathelement
converts the colon-separated list of env.PATH
to a directory search list used by the available
task.
I also test if there is an .svn
directory available. This is to verify that the project being built is from an svn working directory. Obviously svn info
would fail otherwise.
If these two conditions are met, the can.do.svn
property is set to true and allows the svnInfo
target to run when called as a dependancy of buildInfo
. I’m defining canDoSvn
as a dependancy rather than calling it directly with antcall
because with the latter method the can.do.svn
property is not accessible outside the canDoSvn
target, so the svnInfo
condition is never met.
To be thorough, I still need to add a condition that the svn file is executable. I’m not sure how to do that yet.
In my case, I’m guaranteed the ant build is executing on a Linux system, so I don’t know or have to worry about what will happen if this technique is attempted in MS Windows.
Update: From the documentation, it looks like the svn
task does indeed interface the the svn info
command, using the status
element. Anyhoo, I don’t have the svn task installed on our systems so wrapping the svn
command line stands.