You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by Andy Stevens <in...@googlemail.com> on 2009/09/08 08:41:36 UTC

calculating relative paths

Hi,

Is there an easy way to calculate an arbitrary relative path between two files?
Part of my Ant script copies a default configuration file to a build
folder, in the course of which I modify it (with xmltask) with the
location of some source files.  At the moment I'm using <property
location="${src.dir}" .../> to get the absolute path, but I'm curious
whether there's a way to get a relative path instead .  Since I know
where it's being copied to, I could just use "../../${src.dir}", but
that's a bit fragile if the build directory changes (which it will do
- locally on my machine I use ${basedir}/build, but on our CI server
it's likely to override this with some other directory).
Ideally, I suppose I'd like to use something like <property
location="${src.dir}" basedir="${build.dir}" .../>  Is there anything
like this in one of the "extras" libraries, or some other easy way to
achieve it?


Andy

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: calculating relative paths

Posted by Andy Stevens <in...@googlemail.com>.
2009/9/9  <Ja...@rzf.fin-nrw.de>:
> The enhancement of <property location/> seems to be good candidate.
...
> <property location="my/file.txt" relative="true" basedir="my/dir"/>
> --> ../my/file.txt

../file.txt, surely?  my/dir + .. -> my, my/dir + ../my -> my/my,
my/dir + ../my/file.txt -> my/my/file.txt

You might also like to add a test case for

<property location="dir1/file.txt" relative="true" basedir="dir2"/>
--> ../dir1/file.txt

which I don't think is covered by the others.  Also, I notice all the
test cases you mentioned have a file as the location; I assume it'll
work the same way when location is a directory?  In which case, there
probably ought to be a test case for

<property location="my/dir" relative="true" basedir="my/dir"/>

probably expecting "." to be returned, though you could make a
reasonable argument in favour of "" instead.

Actually, that gives me a thought about the extra "../" I was seeing
with your earlier FileUtils.getRelativePath suggestion - could it be
that it assumes the first argument (the base for the relative path) is
always a directory?  That would explain why foo/1 -> foo/2 is returned
as ../2 rather than just 2, it's actually doing foo/1/ -> foo/2.  In
which case, your use of baseFile in the scriptdef was slightly
misleading, baseDir would have been a better name.
Alternatively, perhaps getRelativePath ought to check
File.isDirectory()/File.isFile() and adjust accordingly?


Andy
-- 
http://pseudoq.sourceforge.net/  Open source java sudoku application

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


AW: calculating relative paths

Posted by Ja...@rzf.fin-nrw.de.
I added that in rev812881.
So it should be available with Ant 1.8.0 this year.

Jan

  <!-- Ensure we have platform dependent path separator -->
  <property name="base" location="${basedir}"/>
  <!-- an existing file as anchor for calculating paths -->
  <property name="testfile" value="condition${file.separator}antversion-test.xml"/>

  <target name="testLocation">
    <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> 

>-----Ursprüngliche Nachricht-----
>Von: Jan.Materne@rzf.fin-nrw.de [mailto:Jan.Materne@rzf.fin-nrw.de] 
>Gesendet: Mittwoch, 9. September 2009 11:30
>An: user@ant.apache.org
>Betreff: AW: calculating relative paths
>
>>Perhaps I should raise an enhancement request.  A couple of new
>>attributes "relative" (default false) and "basedir" (default
>>${basedir}) that can be used in conjunction with the "location"
>>attribute seem a relatively minor addition, and would reduce my
>>problem to <property name="relpath" location="${src.dir}"
>>relative="true" basedir="${build.dir}/xsltdoc"/>
>>Any of the developers care to comment?
>
>The enhancement of <property location/> seems to be good candidate.
>
>
><property value="my/file.txt"/>
>-->  my/file.txt
>
><property location="my/file.txt"/>
>--> path/to/projectbasedir/my/file.txt
>
><property location="my/file.txt" relative="true"/>
>--> my/file.txt
>
><property location="my/file.txt" relative="true" basedir="my"/>
>--> file.txt
>
><property location="my/file.txt" relative="true" basedir="my/dir"/>
>--> ../my/file.txt
>
><property location="my/file.txt" relative="true" basedir=".."/>
>--> projectbasedir/my/file.txt
>
>
>Jan
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
>For additional commands, e-mail: user-help@ant.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


AW: calculating relative paths

Posted by Ja...@rzf.fin-nrw.de.
>Perhaps I should raise an enhancement request.  A couple of new
>attributes "relative" (default false) and "basedir" (default
>${basedir}) that can be used in conjunction with the "location"
>attribute seem a relatively minor addition, and would reduce my
>problem to <property name="relpath" location="${src.dir}"
>relative="true" basedir="${build.dir}/xsltdoc"/>
>Any of the developers care to comment?

The enhancement of <property location/> seems to be good candidate.


<property value="my/file.txt"/>
-->  my/file.txt

<property location="my/file.txt"/>
--> path/to/projectbasedir/my/file.txt

<property location="my/file.txt" relative="true"/>
--> my/file.txt

<property location="my/file.txt" relative="true" basedir="my"/>
--> file.txt

<property location="my/file.txt" relative="true" basedir="my/dir"/>
--> ../my/file.txt

<property location="my/file.txt" relative="true" basedir=".."/>
--> projectbasedir/my/file.txt


Jan

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: calculating relative paths

Posted by Andy Stevens <in...@googlemail.com>.
2009/9/8 Francis GALIEGUE <fg...@one2team.com>:
> On Tue, Sep 8, 2009 at 21:03, Andy
> Stevens<in...@googlemail.com> wrote:
>> 2009/9/8 Francis GALIEGUE <fg...@one2team.com>:
>>> On Tue, Sep 8, 2009 at 08:41, Andy
>>> Stevens<in...@googlemail.com> wrote:
>>>> Hi,
>>>>
>>>> Is there an easy way to calculate an arbitrary relative path between two files?
>>>> Part of my Ant script copies a default configuration file to a build
>>>> folder, in the course of which I modify it (with xmltask) with the
>>>> location of some source files.  At the moment I'm using <property
>>>> location="${src.dir}" .../> to get the absolute path, but I'm curious
>>>> whether there's a way to get a relative path instead .  Since I know
>>>> where it's being copied to, I could just use "../../${src.dir}", but
>>>> that's a bit fragile if the build directory changes (which it will do
>>>> - locally on my machine I use ${basedir}/build, but on our CI server
>>>> it's likely to override this with some other directory).
>>>> Ideally, I suppose I'd like to use something like <property
>>>> location="${src.dir}" basedir="${build.dir}" .../>  Is there anything
>>>> like this in one of the "extras" libraries, or some other easy way to
>>>> achieve it?
>>>
>>> You can use ant-contrib's <propertyregex>:
>>>
>>> <propertyregex property="relativepath"
>>>    input="${src.dir}" regexp="^${build.dir}/" replace=""/>
>>
>> Unless I'm missing something, that only works if src.dir is below
>> build.dir?  Which is unlikely, as build.dir (by default
>> ${basedir}/build and its subdirectories) only gets created during the
>> build while src.dir (${basedir}/src) already exists...
>
> Well, instead of <property location=""> you can use <property
> value="">, which will be a relative path to your basedir.

which is already the case.

> You can then prepend the build.dir property without even using a
> regex, like in "${build.dir}/${src.dir}".

${build.dir}/${src.dir} = build/src so the path the XSL stylesheets
will then use (which is taken as relative to the XML file in
build/xsltdoc) would resolve to ${basedir}/build/xsltdoc/build/src not
${basedir}/src

Jan's solution using FileUtils.getRelativePath looks the most
promising if there's no other simple method, other than the extra ../
of course.

Perhaps I should raise an enhancement request.  A couple of new
attributes "relative" (default false) and "basedir" (default
${basedir}) that can be used in conjunction with the "location"
attribute seem a relatively minor addition, and would reduce my
problem to <property name="relpath" location="${src.dir}"
relative="true" basedir="${build.dir}/xsltdoc"/>
Any of the developers care to comment?


Andy
-- 
http://pseudoq.sourceforge.net/

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: calculating relative paths

Posted by Francis GALIEGUE <fg...@one2team.com>.
On Tue, Sep 8, 2009 at 21:03, Andy
Stevens<in...@googlemail.com> wrote:
> 2009/9/8 Francis GALIEGUE <fg...@one2team.com>:
>> On Tue, Sep 8, 2009 at 08:41, Andy
>> Stevens<in...@googlemail.com> wrote:
>>> Hi,
>>>
>>> Is there an easy way to calculate an arbitrary relative path between two files?
>>> Part of my Ant script copies a default configuration file to a build
>>> folder, in the course of which I modify it (with xmltask) with the
>>> location of some source files.  At the moment I'm using <property
>>> location="${src.dir}" .../> to get the absolute path, but I'm curious
>>> whether there's a way to get a relative path instead .  Since I know
>>> where it's being copied to, I could just use "../../${src.dir}", but
>>> that's a bit fragile if the build directory changes (which it will do
>>> - locally on my machine I use ${basedir}/build, but on our CI server
>>> it's likely to override this with some other directory).
>>> Ideally, I suppose I'd like to use something like <property
>>> location="${src.dir}" basedir="${build.dir}" .../>  Is there anything
>>> like this in one of the "extras" libraries, or some other easy way to
>>> achieve it?
>>
>> You can use ant-contrib's <propertyregex>:
>>
>> <propertyregex property="relativepath"
>>    input="${src.dir}" regexp="^${build.dir}/" replace=""/>
>
> Unless I'm missing something, that only works if src.dir is below
> build.dir?  Which is unlikely, as build.dir (by default
> ${basedir}/build and its subdirectories) only gets created during the
> build while src.dir (${basedir}/src) already exists...
>

Well, instead of <property location=""> you can use <property
value="">, which will be a relative path to your basedir.

You can then prepend the build.dir property without even using a
regex, like in "${build.dir}/${src.dir}".

-- 

Francis Galiegue
ONE2TEAM
Ingénieur système
Mob : +33 (0) 683 877 875
Tel : +33 (0) 178 945 552
fge@one2team.com
40 avenue Raymond Poincaré
75116 Paris

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: calculating relative paths

Posted by Andy Stevens <in...@googlemail.com>.
2009/9/8 Francis GALIEGUE <fg...@one2team.com>:
> On Tue, Sep 8, 2009 at 08:41, Andy
> Stevens<in...@googlemail.com> wrote:
>> Hi,
>>
>> Is there an easy way to calculate an arbitrary relative path between two files?
>> Part of my Ant script copies a default configuration file to a build
>> folder, in the course of which I modify it (with xmltask) with the
>> location of some source files.  At the moment I'm using <property
>> location="${src.dir}" .../> to get the absolute path, but I'm curious
>> whether there's a way to get a relative path instead .  Since I know
>> where it's being copied to, I could just use "../../${src.dir}", but
>> that's a bit fragile if the build directory changes (which it will do
>> - locally on my machine I use ${basedir}/build, but on our CI server
>> it's likely to override this with some other directory).
>> Ideally, I suppose I'd like to use something like <property
>> location="${src.dir}" basedir="${build.dir}" .../>  Is there anything
>> like this in one of the "extras" libraries, or some other easy way to
>> achieve it?
>
> You can use ant-contrib's <propertyregex>:
>
> <propertyregex property="relativepath"
>    input="${src.dir}" regexp="^${build.dir}/" replace=""/>

Unless I'm missing something, that only works if src.dir is below
build.dir?  Which is unlikely, as build.dir (by default
${basedir}/build and its subdirectories) only gets created during the
build while src.dir (${basedir}/src) already exists...


Andy
-- 
http://pseudoq.sourceforge.net/

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: calculating relative paths

Posted by Francis GALIEGUE <fg...@one2team.com>.
On Tue, Sep 8, 2009 at 08:41, Andy
Stevens<in...@googlemail.com> wrote:
> Hi,
>
> Is there an easy way to calculate an arbitrary relative path between two files?
> Part of my Ant script copies a default configuration file to a build
> folder, in the course of which I modify it (with xmltask) with the
> location of some source files.  At the moment I'm using <property
> location="${src.dir}" .../> to get the absolute path, but I'm curious
> whether there's a way to get a relative path instead .  Since I know
> where it's being copied to, I could just use "../../${src.dir}", but
> that's a bit fragile if the build directory changes (which it will do
> - locally on my machine I use ${basedir}/build, but on our CI server
> it's likely to override this with some other directory).
> Ideally, I suppose I'd like to use something like <property
> location="${src.dir}" basedir="${build.dir}" .../>  Is there anything
> like this in one of the "extras" libraries, or some other easy way to
> achieve it?
>
>

You can use ant-contrib's <propertyregex>:

<propertyregex property="relativepath"
    input="${src.dir}" regexp="^${build.dir}/" replace=""/>

-- 

Francis Galiegue
ONE2TEAM
Ingénieur système
Mob : +33 (0) 683 877 875
Tel : +33 (0) 178 945 552
fge@one2team.com
40 avenue Raymond Poincaré
75116 Paris

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: calculating relative paths

Posted by Andy Stevens <in...@googlemail.com>.
2009/9/8 Jan.Materne <Ja...@rzf.fin-nrw.de>:
> I am not aware of any built in task, but there is a method in FileUtils you could use.
>
> Jan
>
>        <scriptdef name="relpath" language="javascript">
>            <attribute name="property"/>
>            <attribute name="base"/>
>            <attribute name="file"/>
>            <![CDATA[
>                importClass(Packages.org.apache.tools.ant.util.FileUtils);
>                importClass(Packages.java.io.File);
>                propName = attributes.get("property");
>                baseFile = attributes.get("base");
>                file     = attributes.get("file");
>                relPath  = FileUtils.getRelativePath(new File(baseFile), new File(file) );
>                project.setNewProperty(propName, relPath);
>            ]]>
>        </scriptdef>
>        <relpath property="rel" base="x/a/b" file="x/a/b/c/d/e/f/g/file.txt"/>

Strange, that looks like it ought to be just the job, but with
  build.dir=build
  build.xsltdoc.dir=${build.dir}/xsltdoc
  build.xsltdoc.config=${build.xsltdoc.dir}/XSLTdocConfig.xml
  dist.dir=dist
  dist.xsltdoc.dir=${dist.dir}/xsltdoc
  src.dir=src
then
        <relpath property="xsltdoc.config.target"
base="${build.xsltdoc.config}" file="${dist.xsltdoc.dir}"/>
        <relpath property="xsltdoc.config.source"
base="${build.xsltdoc.config}" file="${src.dir}"/>
results in
  xsltdoc.config.target=../../../dist/xsltdoc
  xsltdoc.config.source=../../../src
which is one "../" too many in each case :-(
A bug in FileUtils.getRelativePath, perhaps?
        <relpath property="relpath.test" base="${build.xsltdoc.dir}/1"
file="${build.xsltdoc.dir}/2"/>
        <echo message="relpath.test=${relpath.test}"/>
or even
        <relpath property="relpath.test" base="1" file="2"/>
both display
  relpath.test=../2
when I'd have expected just "2".


Andy
-- 
http://pseudoq.sourceforge.net/  Open source java sudoku solver

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


AW: calculating relative paths

Posted by Ja...@rzf.fin-nrw.de.
I am not aware of any built in task, but there is a method in FileUtils you could use.

Jan

        <scriptdef name="relpath" language="javascript">
            <attribute name="property"/>
            <attribute name="base"/>
            <attribute name="file"/>
            <![CDATA[
                importClass(Packages.org.apache.tools.ant.util.FileUtils);
                importClass(Packages.java.io.File);
                propName = attributes.get("property");
                baseFile = attributes.get("base");
                file     = attributes.get("file");
                relPath  = FileUtils.getRelativePath(new File(baseFile), new File(file) );
                project.setNewProperty(propName, relPath);
            ]]>
        </scriptdef>
        <relpath property="rel" base="x/a/b" file="x/a/b/c/d/e/f/g/file.txt"/>


or as java code (task)
        <taskdef name="relpath" classname="RelpathTask" classpath="."/>
        <relpath property="rel" base="x/a/b" file="x/a/b/c/d/e/f/g/file.txt"/>



import java.io.File;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.util.FileUtils;

public class RelpathTask extends Task {
    private File basefile;
    private File checkfile;
    private String propertyname;
    
    public void setBase(File base) {
        basefile = base;
    }
    
    public void setFile(File file) {
        checkfile = file;
    }
    
    public void setProperty(String propName) {
        propertyname = propName;
    }
    
    public void execute() {
        checkSettings();
        try {
            String relPath = FileUtils.getRelativePath(basefile, checkfile);
            getProject().setNewProperty(propertyname, relPath);
        } catch (Exception e) {
            throw new BuildException(e);
        }
    }
    
    protected void checkSettings() {
        if (basefile == null) {
            throw new BuildException("'basefile' must be set.", getLocation());
        }
        if (checkfile == null) {
            throw new BuildException("'checkfile' must be set.", getLocation());
        }
        if (propertyname == null) {
            throw new BuildException("'propertyname' must be set.", getLocation());
        }
    }
} 

>-----Ursprüngliche Nachricht-----
>Von: Andy Stevens [mailto:insomniacpenguin@googlemail.com] 
>Gesendet: Dienstag, 8. September 2009 08:42
>An: user
>Betreff: calculating relative paths
>
>Hi,
>
>Is there an easy way to calculate an arbitrary relative path 
>between two files?
>Part of my Ant script copies a default configuration file to a build
>folder, in the course of which I modify it (with xmltask) with the
>location of some source files.  At the moment I'm using <property
>location="${src.dir}" .../> to get the absolute path, but I'm curious
>whether there's a way to get a relative path instead .  Since I know
>where it's being copied to, I could just use "../../${src.dir}", but
>that's a bit fragile if the build directory changes (which it will do
>- locally on my machine I use ${basedir}/build, but on our CI server
>it's likely to override this with some other directory).
>Ideally, I suppose I'd like to use something like <property
>location="${src.dir}" basedir="${build.dir}" .../>  Is there anything
>like this in one of the "extras" libraries, or some other easy way to
>achieve it?
>
>
>Andy
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
>For additional commands, e-mail: user-help@ant.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: calculating relative paths

Posted by Dominique Devienne <dd...@gmail.com>.
On Tue, Sep 8, 2009 at 1:41 AM, Andy
Stevens<in...@googlemail.com> wrote:
> Is there an easy way to calculate an arbitrary relative path between two files?

Indirectly, with <manifestclasspath>, which computes the rel. path
between a set of files and the jar. May be subverted into doing it I think. --DD

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org