ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Andy Stevens <>
Subject Re: calculating relative paths
Date Tue, 08 Sep 2009 23:43:24 GMT
2009/9/8 Francis GALIEGUE <>:
> On Tue, Sep 8, 2009 at 21:03, Andy
> Stevens<> wrote:
>> 2009/9/8 Francis GALIEGUE <>:
>>> On Tue, Sep 8, 2009 at 08:41, Andy
>>> Stevens<> wrote:
>>>> Hi,
>>>> Is there an easy way to calculate an arbitrary relative path between two
>>>> 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

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?


To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message