ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Otto Perdeck" <>
Subject Where task
Date Mon, 11 Mar 2002 11:50:12 GMT
Hi all,

Here's a simple Ant task that checks for the availability of an executable in the path. I
use it to conditionally build parts of the project depending on which executables are available.
This is useful for us since we run multiple daily builds on different platforms, and not all
the executables are available everywhere.

I post it here because it might be useful to others too. The code is simplistic, improvements
are welcome.

Usage is something like:

<property environment="env"/>
<property name="env.PATH" value="${env.Path}"/> <!-- on Windows, path could be in
PATH as well as in Path -->
<where path="${env.PATH}" file="lcp.bat" property="lcp.isavailable"/>
<where path="${env.PATH}" file="../../ant/bin/lcp.bat" property="lcp.isavailable"/>

-Otto Perdeck

Here's the code:


import java.util.StringTokenizer;

 * Additional Ant task to check for the availability of a 
 * program in the "path".
 * <p>
 * "path" gives the path, typically obtained
 * from as ${env.PATH}.
 * <p>
 * "file" is the name of the file to look for in the given path,
 * add all required extensions for the platform you're using 
 * (e.g. ".bat" or ".exe" for Windows systems). If the name does
 * contain directory elements (either relative or absolute), the
 * path is not used at all, but the file is searched for relative
 * to the current dir.
 * <p>
 * "property" is the property to set when "file" is found. When the
 * file isn't found, the property is not set.
 * <p>
 * E.g.:
 * <pre>
 *   &gt;where path="${env.PATH}" file="lcp.bat" property="lcp.isavailable"/&lt;
 *   &gt;where path="${env.PATH}" file="../../ant/bin/lcp.bat" property="lcp.isavailable"/&lt;
 * </pre>
public class Where extends Task
    private String path;
    private String file;
    private String propertyName;

    // The setter for the "path" attribute
    public void setPath(String path) {
        this.path = path;

    // The setter for the "file" attribute
    public void setFile(String file) {
        this.file = file;

    // The setter for the "property" attribute
    public void setProperty(String propertyName) {
        this.propertyName = propertyName;

    // The method executing the task
    public void execute() throws BuildException {
        StringTokenizer pathEnum = 
            new StringTokenizer(path, File.pathSeparator);
        while (pathEnum.hasMoreTokens()) {
            String pathElem = pathEnum.nextToken();
            File arg = new File(file);
            File candidate = (arg.getParent() == null) ? 
                new File(pathElem, file) : arg;
            if (candidate.exists()) {
                project.setProperty(propertyName, candidate.toString());

  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message