You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ant.apache.org by jh...@apache.org on 2009/09/09 12:54:28 UTC

svn commit: r812881 - in /ant/core/trunk: docs/manual/CoreTasks/property.html src/main/org/apache/tools/ant/taskdefs/Property.java src/tests/antunit/taskdefs/property-test.xml

Author: jhm
Date: Wed Sep  9 10:54:28 2009
New Revision: 812881

URL: http://svn.apache.org/viewvc?rev=812881&view=rev
Log:
Calculate relative paths.

Modified:
    ant/core/trunk/docs/manual/CoreTasks/property.html
    ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Property.java
    ant/core/trunk/src/tests/antunit/taskdefs/property-test.xml

Modified: ant/core/trunk/docs/manual/CoreTasks/property.html
URL: http://svn.apache.org/viewvc/ant/core/trunk/docs/manual/CoreTasks/property.html?rev=812881&r1=812880&r2=812881&view=diff
==============================================================================
--- ant/core/trunk/docs/manual/CoreTasks/property.html (original)
+++ ant/core/trunk/docs/manual/CoreTasks/property.html Wed Sep  9 10:54:28 2009
@@ -33,7 +33,7 @@
  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 @@
       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 @@
 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

Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Property.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Property.java?rev=812881&r1=812880&r2=812881&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Property.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Property.java Wed Sep  9 10:54:28 2009
@@ -92,6 +92,8 @@
     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
@@ -125,6 +127,24 @@
     }
 
     /**
+     * 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 @@
      * @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 @@
         }
 
         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 @@
             }
         }
     }
-
+    
     /**
      * load properties from a url
      * @param url url to load from

Modified: ant/core/trunk/src/tests/antunit/taskdefs/property-test.xml
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/tests/antunit/taskdefs/property-test.xml?rev=812881&r1=812880&r2=812881&view=diff
==============================================================================
--- ant/core/trunk/src/tests/antunit/taskdefs/property-test.xml (original)
+++ ant/core/trunk/src/tests/antunit/taskdefs/property-test.xml Wed Sep  9 10:54:28 2009
@@ -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>