On Jan 2, 2008 4:03 PM, Francisco Tolmasky <[EMAIL PROTECTED]> wrote:
> I am running into a problem when outputing files using an identity
> mapper, since sometimes the
> relative paths don't exist.  For example, I use GCC in ant as so:
>
>          <apply executable = "gcc" dest = "whatever" >
>
>              <targetfile/>
>              <srcfile/>
>
>              <fileset refid = "Sources" />
>              <identitymapper/>
>
>          </apply>
>
> When I run this, if there is a file in "Sources" such as  "Directory/
> Directory/A", I get that
> "whatever/Directory/Directory" doesn't exist, and rightfully so.
> However, is there any way to just
> have these intermediary directories created for me?  Or some "create
> every possible directory"
> task I can use?

I've run into this issue too, a while back. What I did to solve it was
to create my own <apply> which derived from Ant's own <apply>, adding
the directory-creation logic just-in-time. That involves some
coding/compiling/packaging, which may or not be an issue for you, but
it works. --DD

package whatever;

import java.io.File;

import org.apache.tools.ant.Task;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.taskdefs.ExecuteOn;

/**
 * Wrapper around Ant's &lt;apply&gt; that creates target directories.
 */
public class Apply extends ExecuteOn {

    /**
     * Construct the command line for serial execution,
     * creating any necessary output directories.
     *
     * @param srcFile The filename to add to the commandline
     * @param baseDir filename is relative to this dir
     * @return the serial command line
     *
     * @see #createDestinationDirectory
     */
    protected String[] getCommandline(String srcFile, File baseDir) {
        // Done in parallel's getCommandline, but done here too just in case
        createDestinationDirectory(srcFile);
        return super.getCommandline(srcFile, baseDir);
    }

    /**
     * Construct the command line for parallel execution.
     *
     * @param srcFiles The filenames to add to the commandline
     * @param baseDirs filenames are relative to this dir
     * @return the parallel command line
     *
     * @see #createDestinationDirectory
     */
    protected String[] getCommandline(String[] srcFiles, File[] baseDirs) {
        for (int i = 0; i < srcFiles.length; ++i) {
            createDestinationDirectory(srcFiles[i]);
        }
        return super.getCommandline(srcFiles, baseDirs);
    }

    /**
     * Creates the destination directories for a given source file.
     * <p>
     * If a [EMAIL PROTECTED] #setDest destination directory} and a
     * [EMAIL PROTECTED] #createMapper filename mapper} have been specified, the
     * target files' destination directories are created.
     *
     * @param  srcFile the source file's relative filename.
     * @throws BuildException if a destination directory could not be created.
     */
    protected void createDestinationDirectory(String srcFile) {
        if (this.mapper == null || this.destDir == null) {
            return;
        }

        String[] targets = this.mapper.mapFileName(srcFile);
        if (targets == null || targets.length < 1) {
            return;
        }

        for (int i = 0; i < targets.length; ++i) {
            String target = targets[i];
            File targetFile = new File(this.destDir, target);
            File destination = targetFile.getParentFile();
            if (!destination.exists()) {
                log("Creating dest. dir: " + destination, Project.MSG_DEBUG);
                if (!destination.mkdirs()) {
                    throw new BuildException("Cannot create dest. dir " +
                      destination + " of source file " + srcFile);
                }
            }
        }
    }

}

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to