In ant, I have defined some properties that define some paths (absolute) needed in my build system.
Most of ant tasks need filesets, so to build a fileset from a property I would have to do:
<fileset dir="" includes="${myprop}"/>
The problem is that myprop
is absolute and I should omit the dir attribute (which is not possible), so is there a way to define (absolute) properties and efficiently use them to create filesets, dirsets, etc.. or in ant is this a bad practice (only relative paths should be used)?
Thanks.
There's no particular rules about absolute versus relative with Ant. You should avoid hard-coding absolute paths if possible - to make your build more portable. If you have to use absolute paths for some part of the build, try to construct them - say from the
${basedir}
of the project - so that changes are easier to make.Two things that may be useful for converting absolute paths to relative paths for use in filesets and the like:
property
task supports an attributerelative=
that can be used to convert from an absolute path to relative.pathconvert
task can be used to do the same, when there are several absolute paths in one property.Examples:
In response to a comment: I'm not aware of a simple way to get the longest common prefix directory of a fileset in Ant, but here's a macro wrapping a script task that does what you might call 'shrink wrapping'. You call it with a reference to the 'input' fileset, and the name of the property to set.
(This was derived from a php implementation.)
You can try it out with something like:
The
pathconvert
task can now be used to create a shrink-wrapped fileset:I would suggest you to use a different property for the
dir
target, if you are doing an absolute reference to ${myprop}, I guess it's a file name. Try it separating it into ${parent.dir} and ${target.filename}.