ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dominique Devienne <ddevie...@gmail.com>
Subject Re: apply task trouble for command using < and >
Date Sat, 26 Nov 2005 03:21:55 GMT
> Hmm, just one small problem I couldn't solve yet.  Apply will bail out
> if it tries to output a file to a directory that doesn't exist.

I ran into the same issue in the past, and had to "patch" <apply> to
solve it, as you don't know "a priori" which directories to create
with using a destDir and a mapper.

I'll try to update the "real" apply task next week, but you can use
the custom task below in the mean time (it just "plugs in" at the
right time to create the directories, without duplicating the <apply>
code itself. Ant's <apply> maps to ExecuteOn).

--DD


package buildmagic;

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 {@link #setDest destination directory} and a
     * {@link #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: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Mime
View raw message