View | Details | Raw Unified | Return to bug 21494
Collapse All | Expand All

(-)src/main/org/apache/tools/ant/taskdefs/Property.java (-1 / +48 lines)
Lines 64-77 Link Here
64
import java.util.Vector;
64
import java.util.Vector;
65
import org.apache.tools.ant.BuildException;
65
import org.apache.tools.ant.BuildException;
66
import org.apache.tools.ant.Project;
66
import org.apache.tools.ant.Project;
67
import org.apache.tools.ant.ProjectHelper;
67
import org.apache.tools.ant.ProjectHelper;
68
import org.apache.tools.ant.Task;
68
import org.apache.tools.ant.Task;
69
import org.apache.tools.ant.types.Path;
69
import org.apache.tools.ant.types.Path;
70
import org.apache.tools.ant.types.Reference;
70
import org.apache.tools.ant.types.Reference;
71
import org.apache.tools.ant.util.FileUtils;
71
72
72
/**
73
/**
73
 * Sets a property by name, or set of properties (from file or
74
 * Sets a property by name, or set of properties (from file or
74
 * resource) in the project.  </p>
75
 * resource) in the project.  </p>
75
 * Properties are immutable: whoever sets a property first freezes it for the
76
 * Properties are immutable: whoever sets a property first freezes it for the
76
 * rest of the build; they are most definately not variable.
77
 * rest of the build; they are most definately not variable.
77
 * <p>There are five ways to set properties:</p>
78
 * <p>There are five ways to set properties:</p>
Lines 161-174 Link Here
161
     * @ant.attribute group="name"
162
     * @ant.attribute group="name"
162
     */
163
     */
163
    public void setLocation(File location) {
164
    public void setLocation(File location) {
164
        setValue(location.getAbsolutePath());
165
        setValue(location.getAbsolutePath());
165
    }
166
    }
166
167
167
    /**
168
    /**
169
     * If fileName is a relative path, search project's basedir or its parent 
170
     * directories for the file ant set the property to the absolute
171
     * path of the found file. If file can not be found, assume it specifies
172
     * new file in project's basedir and set the property to the resulting
173
     * absolute path.
174
     * If fileName is an absolute path, set property to its path.
175
     * @param fileName file name to search
176
     *
177
     * @ant.attribute group="name"
178
     */
179
    public void setSearchparents(String fileName) {
180
        File result = search(getProject().getBaseDir(), fileName);
181
        setValue(result.getAbsolutePath());
182
    }
183
184
    /**
185
     * Search for the given path in startDir or one of its parents.
186
     */
187
    private File search(File startDir, String path) {
188
        FileUtils fu = FileUtils.newFileUtils();
189
190
        File initial = fu.resolveFile(startDir, path);
191
        if (initial.exists()) {
192
            log("Found file: "+initial, Project.MSG_VERBOSE);
193
            return initial;
194
        }
195
        if (!(new File(path).isAbsolute())) {
196
            File dir = startDir;
197
            for (;;) {
198
                dir = fu.getParentFile(dir);
199
                if (dir == null) {
200
                    break;
201
                }
202
                File test = fu.resolveFile(dir, path);
203
                if (test.exists()) {
204
                    log("Found file: "+initial, Project.MSG_VERBOSE);
205
                    return test;
206
                }
207
            }
208
        }
209
        log("Using non-existing file: "+initial, Project.MSG_VERBOSE);
210
        return initial;
211
    }
212
213
    /**
168
     * The value of the property.
214
     * The value of the property.
169
     * @param value value to assign
215
     * @param value value to assign
170
     *
216
     *
171
     * @ant.attribute group="name"
217
     * @ant.attribute group="name"
172
     */
218
     */
173
    public void setValue(String value) {
219
    public void setValue(String value) {
174
        this.value = value;
220
        this.value = value;
Lines 352-366 Link Here
352
    public void execute() throws BuildException {
398
    public void execute() throws BuildException {
353
        if (getProject() == null) {
399
        if (getProject() == null) {
354
            throw new IllegalStateException("project has not been set");
400
            throw new IllegalStateException("project has not been set");
355
        }
401
        }
356
402
357
        if (name != null) {
403
        if (name != null) {
358
            if (value == null && ref == null) {
404
            if (value == null && ref == null) {
359
                throw new BuildException("You must specify value, location or "
405
                throw new BuildException("You must specify value, location, "
406
                                         + "searchparents or "
360
                                         + "refid with the name attribute",
407
                                         + "refid with the name attribute",
361
                                         getLocation());
408
                                         getLocation());
362
            }
409
            }
363
        } else {
410
        } else {
364
            if (url == null && file == null && resource == null && env == null) {
411
            if (url == null && file == null && resource == null && env == null) {
365
                throw new BuildException("You must specify url, file, resource or "
412
                throw new BuildException("You must specify url, file, resource or "
366
                                         + "environment when not using the "
413
                                         + "environment when not using the "

Return to bug 21494