ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From <>
Subject AW: calculating relative paths
Date Tue, 08 Sep 2009 07:34:23 GMT
I am not aware of any built in task, but there is a method in FileUtils you could use.


        <scriptdef name="relpath" language="javascript">
            <attribute name="property"/>
            <attribute name="base"/>
            <attribute name="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);
        <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"/>


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() {
        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 [] 
>Gesendet: Dienstag, 8. September 2009 08:42
>An: user
>Betreff: calculating relative paths
>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?
>To unsubscribe, e-mail:
>For additional commands, e-mail:

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

View raw message