How to strip the basedir from an absolute path to get a relative path?
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
.
What I need is just the relative path ./my_project/some_dir
without the ${basedir}
value /home/myuser
. How can I achieve this using Ant?
So far I found a solu开发者_Python百科tion 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?
Since Ant 1.8.0 you can use the relative
attribute of the Ant property
task for this.
For example:
<property name="somedir.dir" location="my_project/some_dir"/>
<echo message="${somedir.dir}" />
<property name="somedir.rel" value="${somedir.dir}" relative="yes" />
<echo message="${somedir.rel}" />
Leads to:
[echo] /home/.../stack_overflow/ant/my_project/some_dir
[echo] my_project/some_dir
A slightly less verbose solution would be specifying somepath
inside <pathconvert>
:
<pathconvert property="relative.dir">
<path location="${somepath}"/>
<globmapper from="${basedir}/*" to="./*" />
</pathconvert>
You might be able to use the Ant 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.
This was the approach that worked well for me in Windows, adapted from @Garns answer:
<path id="uploadFilePath">
<fileset dir="${wcm.folderName}">
<include name="*" />
<exclude name="*.attr" />
</fileset>
</path>
<pathconvert property="relFilelist">
<path refid="uploadFilePath" />
<mapper>
<globmapper from="${wcm.folderName}/*" to="*" handledirsep="true" />
</mapper>
</pathconvert>
I think that handledirsep="true" is pretty important in globmapper, it didn't work for me otherwise no matter how much I fiddled with forward/backslashes. (I do all my slashes as forward in my ant scripts just so I can run them on unixy systems.) My version of Ant is 1.8.1.
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"/>
精彩评论