From b482bf23bd33f97ec56d7b41cbbb203f151465fc Mon Sep 17 00:00:00 2001
From: Diane Holt Sets a property if a resource is available at runtime. This resource can be a
file, a directory, a class in the classpath, or a JVM system resource. If the resource is present, the property value is set to true by
-default, otherwise the property is not set. You can set the value to
-something specific by specifying the value
attribute.value
attribute.
Normally, this task is used to set properties that are useful to avoid target execution depending on system parameters.
classname
or resource
.classname
or resource
.file
to look for, either a directory (type="dir"
) or a file (type="file"
). If not set, the property will be set if the name specified in the file
attribute exists as either a file or a directory.org.whatever.Myclass
is found in Ant's classpath.
sets the jaxp.jar.present
property to the value "true"
if the file ./lib/jaxp11/jaxp.jar
is found.
+<available file="/usr/local/lib" type="dir" property="local.lib.present"/> ++
sets the local.lib.present
property to the value "true"
+if the directory /usr/local/lib
is found.
...in project ... <property name="jaxp.jar" value="./lib/jaxp11/jaxp.jar"/> <path id="jaxp" location="${jaxp.jar}"/> diff --git a/src/etc/testcases/taskdefs/available.xml b/src/etc/testcases/taskdefs/available.xml index 97f9b92d4..2b215d5e7 100644 --- a/src/etc/testcases/taskdefs/available.xml +++ b/src/etc/testcases/taskdefs/available.xml @@ -80,17 +80,17 @@+ file="" type="dir"/> + file="../taskdefs" type="dir"/> diff --git a/src/main/org/apache/tools/ant/taskdefs/Available.java b/src/main/org/apache/tools/ant/taskdefs/Available.java index 0dab4f376..7e6d94bc7 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Available.java +++ b/src/main/org/apache/tools/ant/taskdefs/Available.java @@ -70,8 +70,8 @@ public class Available extends Task { private String property; private String classname; private File file; - private File dir; private String resource; + private String type; private Path classpath; private AntClassLoader loader; private String value = "true"; @@ -113,21 +113,27 @@ public class Available extends Task { this.file = file; } - public void setDir(File dir) { - this.dir = dir; - } - public void setResource(String resource) { this.resource = resource; } + public void setType(String type) { + this.type = type; + } + public void execute() throws BuildException { if (property == null) { throw new BuildException("property attribute is required", location); } - if (classname == null && file == null && dir == null && resource == null) { - throw new BuildException("At least one of (classname|file|dir|resource) is required", location); + if (classname == null && file == null && resource == null) { + throw new BuildException("At least one of (classname|file|resource) is required", location); + } + + if (type != null){ + if (!type.equalsIgnoreCase("file") && !type.equalsIgnoreCase("dir")){ + throw new BuildException("Type must be one of either dir or file"); + } } if (classpath != null) { @@ -140,12 +146,7 @@ public class Available extends Task { } if ((file != null) && !checkFile(file)) { - log("Unable to find file " + file + " to set property " + property, Project.MSG_VERBOSE); - return; - } - - if ((dir != null) && !checkDir(dir)) { - log("Unable to find dir " + dir + " to set property " + property, Project.MSG_VERBOSE); + log("Unable to find " + file + " to set property " + property, Project.MSG_VERBOSE); return; } @@ -158,11 +159,14 @@ public class Available extends Task { } private boolean checkFile(File file) { - return file.isFile(); - } - - private boolean checkDir(File dir) { - return dir.isDirectory(); + if (type != null) { + if (type.equalsIgnoreCase("dir")){ + return file.isDirectory(); + } else if (type.equalsIgnoreCase("file")){ + return file.isFile(); + } + } + return file.exists(); } private boolean checkResource(String resource) { + file="../this_dir_should_never_exist" type="dir"/>