Browse Source

Calculate relative paths.

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@812881 13f79535-47bb-0310-9956-ffa450edef68
master
Jan Materne 15 years ago
parent
commit
5ddb0dfbd9
3 changed files with 91 additions and 4 deletions
  1. +22
    -1
      docs/manual/CoreTasks/property.html
  2. +39
    -3
      src/main/org/apache/tools/ant/taskdefs/Property.java
  3. +30
    -0
      src/tests/antunit/taskdefs/property-test.xml

+ 22
- 1
docs/manual/CoreTasks/property.html View File

@@ -33,7 +33,7 @@ resource) in the project. Properties are case sensitive.</p>
rest of the build; they are most definitely not variables.
<p>There are seven ways to set properties:</p>
<ul>
<li>By supplying both the <i>name</i> and <i>value</i> attribute.</li>
<li>By supplying both the <i>name</i> and one of <i>value</i> or <i>location</i> attribute.</li>
<li>By supplying the <i>name</i> and nested text.</li>
<li>By supplying both the <i>name</i> and <i>refid</i> attribute.</li>
<li>By setting the <i>file</i> attribute with the filename of the property
@@ -154,6 +154,16 @@ SYSTEM).
A "." is appended to the prefix if not specified.</td>
<td align="center" valign="top">No</td>
</tr>
<tr>
<td valign="top">relative</td>
<td valign="top">If set to <tt>true</tt> the relative path to <tt>basedir</tt> is set.</td>
<td align="center" valign="top">No (default=<tt>false</tt>)</td>
</tr>
<tr>
<td valign="top">basedir</td>
<td valign="top">The basedir to calculate the relative path from.</td>
<td align="center" valign="top">No (default=<tt>${basedir}</tt>)</td>
</tr>
</table>

<h3>Parameters specified as nested elements</h3>
@@ -211,6 +221,17 @@ environment variable <tt>STAGE</tt> some or all values could be overwritten, e.g
name for the test server). Finally all these values could be overwritten by personal settings with
a file per user.</p>

<pre>
&lt;property name=&quot;foo&quot; location=&quot;my/file.txt&quot; relative=&quot;true&quot; basedir=&quot;..&quot;/&gt;
</pre>
<p>Stores the relative path in <tt>foo</tt>: projectbasedir/my/file.txt</p>

<pre>
&lt;property name=&quot;foo&quot; location=&quot;my/file.txt&quot; relative=&quot;true&quot; basedir=&quot;cvs&quot;/&gt;
</pre>
<p>Stores the relative path in <tt>foo</tt>: ../my/file.txt</p>


<h3>Property Files</h3>

As stated, this task will load in a properties file stored in the file


+ 39
- 3
src/main/org/apache/tools/ant/taskdefs/Property.java View File

@@ -92,6 +92,8 @@ public class Property extends Task {
private Project fallback;
private Object untypedValue;
private boolean valueAttributeUsed = false;
private boolean relative = false;
private File basedir;

protected boolean userProperty; // set read-only properties
// CheckStyle:VisibilityModifier ON
@@ -124,6 +126,24 @@ public class Property extends Task {
this.fallback = fallback;
}

/**
* Sets 'relative' attribute.
* @param relative new value
* @since Ant 1.8.0
*/
public void setRelative(boolean relative) {
this.relative = relative;
}

/**
* Sets 'basedir' attribute.
* @param basedir new value
* @since Ant 1.8.0
*/
public void setBasedir(File basedir) {
this.basedir = basedir;
}

/**
* The name of the property to set.
* @param name property name
@@ -151,7 +171,11 @@ public class Property extends Task {
* @ant.attribute group="name"
*/
public void setLocation(File location) {
setValue(location.getAbsolutePath());
if (relative) {
internalSetValue(location);
} else {
setValue(location.getAbsolutePath());
}
}

/* the following method is first in source so IH will pick it up first:
@@ -433,7 +457,19 @@ public class Property extends Task {
}

if (name != null && untypedValue != null) {
addProperty(name, untypedValue);
if (relative) {
try {
File from = untypedValue instanceof File ? (File)untypedValue : new File(untypedValue.toString());
File to = basedir != null ? basedir : getProject().getBaseDir();
String relPath = FileUtils.getFileUtils().getRelativePath(to, from);
relPath = relPath.replace('/', File.separatorChar);
addProperty(name, relPath);
} catch (Exception e) {
throw new BuildException(e, getLocation());
}
} else {
addProperty(name, untypedValue);
}
}

if (file != null) {
@@ -466,7 +502,7 @@ public class Property extends Task {
}
}
}
/**
* load properties from a url
* @param url url to load from


+ 30
- 0
src/tests/antunit/taskdefs/property-test.xml View File

@@ -47,4 +47,34 @@
<property name="foo" location="${testfile}"/>
<au:assertPropertyEquals name="foo" value="${base}${file.separator}${testfile}"/>
</target>
<target name="testLocationWithRecursive">
<property name="foo" location="${testfile}" relative="false"/>
<au:assertPropertyEquals name="foo" value="${base}${file.separator}${testfile}"/>
</target>

<target name="testRelative">
<property name="foo" location="${testfile}" relative="true"/>
<au:assertPropertyEquals name="foo" value="${testfile}"/>
</target>

<target name="testRelativeBase">
<property name="foo" location="${testfile}" relative="true" basedir="${base}"/>
<au:assertPropertyEquals name="foo" value="${testfile}"/>
</target>
<target name="testRelativeUnderBase">
<property name="foo" location="${testfile}" relative="true" basedir="condition"/>
<au:assertPropertyEquals name="foo" value="antversion-test.xml"/>
</target>

<target name="testRelativeUnderBase2">
<property name="foo" location="${testfile}" relative="true" basedir="cvs"/>
<au:assertPropertyEquals name="foo" value="..${file.separator}condition${file.separator}antversion-test.xml"/>
</target>

<target name="testRelativeOverBase">
<property name="foo" location="${testfile}" relative="true" basedir=".."/>
<au:assertPropertyEquals name="foo" value="taskdefs${file.separator}${testfile}"/>
</target>
</project>

Loading…
Cancel
Save