ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From peter reilly <peter.rei...@corvil.com>
Subject Re: Converting ${user.dir} into a path relative to ${some.dir}
Date Wed, 02 Apr 2003 09:36:29 GMT
You have to be very careful with the dir attribute of
fileset and the name attribute of the include.
(so for example "./" is not a good idea in name ~)).

Also there is some nice utility code in ant for handling
file names.
I am including a modification of your task to use
fileutils.
If the base is the same as the sub, it throws a build exception.

i.e base dir = 1/2/3 sub dir = 1/2/3/4/5 result will be 4/5
i.e base dir = 1/2/3 sub dir = 1/2/3 result will be build exception

Usage:
  <target name="t">
    <property name="src.dir" value="a"/>
    <property name="build.dir" value="build"/>
    <relativepathproperty 
      subdir="${user.dir}" basedir="${src.dir}" property="rel.dir"/>
    <echo message="${rel.dir}"/>

    <mkdir dir="stuff"/>
    <copy todir="stuff">
      <fileset dir="${src.dir}">
        <include name="${rel.dir}/*"/>
      </fileset>
    </copy>
  </target>

Comment free code follows:

import java.io.File;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.util.FileUtils;

public class RelativePathProperty
    extends Task
{
    private File baseDir;
    private File subDir;
    private String property;

    public void setBaseDir(File baseDir) {
        this.baseDir = baseDir;
    }

    public void setSubDir(File subDir) {
        this.subDir = subDir;
    }

    public void setProperty(String property) {
        this.property = property;
    }

    public void execute() {
        if (subDir == null)
            throw new BuildException("missing subdir");
        if (baseDir == null)
            throw new BuildException("missing basedir");
        if (property == null)
            throw new BuildException("missing property");
        if (! baseDir.isDirectory())
            throw new BuildException(
                baseDir.toString() + " is not a directory");
        if (! subDir.isDirectory())
            throw new BuildException(
                subDir.toString() + " is not a directory");
        FileUtils fileUtils = FileUtils.newFileUtils();
        String baseDirNormal =
            fileUtils.normalize(baseDir.getAbsolutePath()).getAbsolutePath()
            + File.separator;
        
        String subDirNormal =
            fileUtils.normalize(subDir.getAbsolutePath()).getAbsolutePath();
        
        if (! subDirNormal.startsWith(baseDirNormal))
            throw new BuildException("subdir is not a sub dir of basedir");

        getProject().setNewProperty(
            property, subDirNormal.substring(baseDirNormal.length()));
    }
}

Peter.
On Wednesday 02 April 2003 10:31, Tim Gordon wrote:
> Hi
>
> I'm invoking ANT from a subdirectory, getting at the build.xml using
> ant -find.
>
> It would be very useful if I could create a FileSet which selects the files
> that are in my directory. This would allow me to execute something like
>
> <relativepathproperty name="relative.dir" basedir="${src.dir}"
> subdir="${user.dir}"/>
> <copy todir="{build.dir}/stuff>
>   <fileset dir="${src.dir}>
>     <include name="${relative.dir}/*"/>
>   </fileset>
> </copy>
>
> So if ${user.dir} is currently
> c:/sandboxes/dev/project/src/com/mydomain/app/client, the build.xml is
> located in c:/sandboxes/dev/project (basedir = '.') and ${src.dir} is set
> to src, ${relative.dir} should end up as com/mydomain/app/client.
>
> I tried just putting ${user.dir} in place of ${relative.dir} and it doesn't
> work - not sure why.
>
> I wrote an implementation of RelativePathProperty for ANT 1.4.1 - it's
> similar to dirname in 1.5.2 - here's it's execute() method - m_basedir and
> m_subbdir are set as File objects:
>
>     StringBuffer relativePath = new StringBuffer();
>     if (m_basedir.equals(m_subdir))
>     {
>       relativePath.append('.');
>     }
>
>     File search = m_subdir;
>     while (!m_basedir.equals(search) && search != null)
>     {
>       if (relativePath.length() > 0)
>       {
>         relativePath.insert(0, File.separatorChar);
>       }
>       relativePath.insert(0, search.getName());
>       search = search.getParentFile();
>     }
>
>     if (search == null)
>     {
>       throw new BuildException(m_subdir.toString() + " is not a
> subdirectory of " + m_basedir);
>     }
>
>     setValue(relativePath.toString());
>
>     super.execute();
>
> I tried using dirname, but it didn't work for me.
>
> Tim Gordon
>
> Allustra Limited
> 1 Royal Exchange Avenue
> London
> EC3V 3LT
> Tel 020 7464 4190
> Tel 020 7464 4194
> http://www.allustra.com/
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org


Mime
View raw message