diff --git a/WHATSNEW b/WHATSNEW index 2461c218c..10b2135b0 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -875,6 +875,8 @@ Other changes: * The resource collection can now optionally cache its contents. + * A new condition can check whether resources exists. + Changes from Ant 1.7.0 TO Ant 1.7.1 ============================================= diff --git a/docs/manual/CoreTasks/conditions.html b/docs/manual/CoreTasks/conditions.html index e0ad40caf..c09396949 100644 --- a/docs/manual/CoreTasks/conditions.html +++ b/docs/manual/CoreTasks/conditions.html @@ -1057,5 +1057,20 @@ is redundant and will be ignored.

<file file="${file}"/> </islastmodified> + +

resourceexists

+ +

Tests a resource for existance. since Ant 1.8.0

+ +

The actual resource to test is specified as a nested element.

+ +

+ An example: +

+
+<resourceexists>
+  <file file="${file}"/>
+</resourceexists>
+
diff --git a/src/main/org/apache/tools/ant/taskdefs/condition/ResourceExists.java b/src/main/org/apache/tools/ant/taskdefs/condition/ResourceExists.java new file mode 100644 index 000000000..feda65b24 --- /dev/null +++ b/src/main/org/apache/tools/ant/taskdefs/condition/ResourceExists.java @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.tools.ant.taskdefs.condition; + +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.ProjectComponent; +import org.apache.tools.ant.types.Resource; + +/** + * Condition that checks whether a given resource exists. + * + * @since Ant 1.8.0 + */ +public class ResourceExists extends ProjectComponent implements Condition { + private Resource resource; + + /** + * The resource to test. + */ + public void add(Resource r) { + if (resource != null) { + throw new BuildException("only one resource can be tested"); + } + resource = r; + } + + /** + * Argument validation. + */ + protected void validate() throws BuildException { + if (resource == null) { + throw new BuildException("resource is required"); + } + } + + public boolean eval() throws BuildException { + validate(); + return resource.isExists(); + } +} diff --git a/src/main/org/apache/tools/ant/types/conditions/antlib.xml b/src/main/org/apache/tools/ant/types/conditions/antlib.xml index f6cf3eaab..ff407b28b 100644 --- a/src/main/org/apache/tools/ant/types/conditions/antlib.xml +++ b/src/main/org/apache/tools/ant/types/conditions/antlib.xml @@ -76,6 +76,8 @@ classname="org.apache.tools.ant.taskdefs.condition.Os"/> + + + + + + + + + + + + + + + + + + + + + + +