views:

889

answers:

2

In the build.xml of my project I have a property defined:

<property name="somedir.dir" location="my_project/some_dir"/>

The value of "${somedir.dir}" will be an absolute path: "/home/myuser/my_project/some_dir". But what I need is just the relative path "./my_project/some_dir" without the ${basedir} value "/home/myuser". How can I achieve this in Ant?

So far I found a solution by converting the property to a path and then use "pathconvert", but I don't think this is a nice solution:

<path id="temp.path">
    <pathelement location="${somedir.dir}" />
</path>
<pathconvert property="relative.dir" refid="temp.path">
    <globmapper from="${basedir}/*" to="./*" />
</pathconvert>

Any other (more elegant) suggestions? Thanks!

+1  A: 

location expands automatically the path using the project's basedir. So I think value option gives you a better control:

<property name="base.dir" value="/home/myuser"/>

and

<property name="somedir.dir" value="${base.dir}/some_dir"/>
Anonymous
I think you misunderstood my question. I already have a given path "${base.dir}/some_dir" and I need to get the relative path "./some_dir".
blackicecube
Why do you need to extract `some_dir` when you have it into `${somedir.dir}`?
Anonymous
${somedir.dir} would be something like "/home/blah/mydirectory" and what the question is asking, is how to get just "./mydirectory" from it. So you can't just use ${somedir.dir}. In nant there are a bunch of functions built in like path::get-directory-name But I can't seem to find if those exist in Ant. I'm also in need of how to do this.
christophercotton
Yes. But as you can see, I'm using `value` instead of `location`. So, if you set `base.dir` to `/home/blah` and `somedir.dir` to `mydirectory`, you'll have them separately, and the absolute path will be `${base.dir}/${somedir.dir}`. Why compacting the absolute path into `base.dir` with the option location, and then looking for a way to extract the working directory from it?
Anonymous
true if you set the variable ourself, but what if it comes as an output of some other task ?
DavidM
A: 

You might be able to use the http://ant.apache.org/manual/CoreTasks/basename.html Basename task. If you have:

    <property name="somedir" value="/path/to/file/here" />
    <basename file="${somedir}" property="somebasedir" />
    <echo>${somebasedir}</echo>

The value that gets echoed is "here". It only seems to give you the final directory, which might not get enough of what you want.

christophercotton