git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@812881 13f79535-47bb-0310-9956-ffa450edef68master
@@ -33,7 +33,7 @@ resource) in the project. Properties are case sensitive.</p> | |||||
rest of the build; they are most definitely not variables. | rest of the build; they are most definitely not variables. | ||||
<p>There are seven ways to set properties:</p> | <p>There are seven ways to set properties:</p> | ||||
<ul> | <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 the <i>name</i> and nested text.</li> | ||||
<li>By supplying both the <i>name</i> and <i>refid</i> attribute.</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 | <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> | A "." is appended to the prefix if not specified.</td> | ||||
<td align="center" valign="top">No</td> | <td align="center" valign="top">No</td> | ||||
</tr> | </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> | </table> | ||||
<h3>Parameters specified as nested elements</h3> | <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 | name for the test server). Finally all these values could be overwritten by personal settings with | ||||
a file per user.</p> | a file per user.</p> | ||||
<pre> | |||||
<property name="foo" location="my/file.txt" relative="true" basedir=".."/> | |||||
</pre> | |||||
<p>Stores the relative path in <tt>foo</tt>: projectbasedir/my/file.txt</p> | |||||
<pre> | |||||
<property name="foo" location="my/file.txt" relative="true" basedir="cvs"/> | |||||
</pre> | |||||
<p>Stores the relative path in <tt>foo</tt>: ../my/file.txt</p> | |||||
<h3>Property Files</h3> | <h3>Property Files</h3> | ||||
As stated, this task will load in a properties file stored in the file | As stated, this task will load in a properties file stored in the file | ||||
@@ -92,6 +92,8 @@ public class Property extends Task { | |||||
private Project fallback; | private Project fallback; | ||||
private Object untypedValue; | private Object untypedValue; | ||||
private boolean valueAttributeUsed = false; | private boolean valueAttributeUsed = false; | ||||
private boolean relative = false; | |||||
private File basedir; | |||||
protected boolean userProperty; // set read-only properties | protected boolean userProperty; // set read-only properties | ||||
// CheckStyle:VisibilityModifier ON | // CheckStyle:VisibilityModifier ON | ||||
@@ -124,6 +126,24 @@ public class Property extends Task { | |||||
this.fallback = fallback; | 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. | * The name of the property to set. | ||||
* @param name property name | * @param name property name | ||||
@@ -151,7 +171,11 @@ public class Property extends Task { | |||||
* @ant.attribute group="name" | * @ant.attribute group="name" | ||||
*/ | */ | ||||
public void setLocation(File location) { | 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: | /* 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) { | 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) { | if (file != null) { | ||||
@@ -466,7 +502,7 @@ public class Property extends Task { | |||||
} | } | ||||
} | } | ||||
} | } | ||||
/** | /** | ||||
* load properties from a url | * load properties from a url | ||||
* @param url url to load from | * @param url url to load from | ||||
@@ -47,4 +47,34 @@ | |||||
<property name="foo" location="${testfile}"/> | <property name="foo" location="${testfile}"/> | ||||
<au:assertPropertyEquals name="foo" value="${base}${file.separator}${testfile}"/> | <au:assertPropertyEquals name="foo" value="${base}${file.separator}${testfile}"/> | ||||
</target> | </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> | </project> |