I am not aware of any built in task, but there is a method in FileUtils you
could use.
Jan
<scriptdef name="relpath" language="javascript">
<attribute name="property"/>
<attribute name="base"/>
<attribute name="file"/>
<![CDATA[
importClass(Packages.org.apache.tools.ant.util.FileUtils);
importClass(Packages.java.io.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);
]]>
</scriptdef>
<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"/>
import java.io.File;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.util.FileUtils;
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() {
checkSettings();
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 [mailto:[email protected]]
>Gesendet: Dienstag, 8. September 2009 08:42
>An: user
>Betreff: calculating relative paths
>
>Hi,
>
>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?
>
>
>Andy
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: [email protected]
>For additional commands, e-mail: [email protected]
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]