Search in sources :

Example 6 with FileProvider

use of org.apache.tools.ant.types.resources.FileProvider in project ant by apache.

the class Echo method setOutput.

/**
 * Resource to write to.
 * @param output the Resource to write to.
 * @since Ant 1.8
 */
public void setOutput(Resource output) {
    if (this.output != null) {
        throw new BuildException("Cannot set > 1 output target");
    }
    this.output = output;
    FileProvider fp = output.as(FileProvider.class);
    this.file = fp != null ? fp.getFile() : null;
}
Also used : FileProvider(org.apache.tools.ant.types.resources.FileProvider) BuildException(org.apache.tools.ant.BuildException)

Example 7 with FileProvider

use of org.apache.tools.ant.types.resources.FileProvider in project ant by apache.

the class ExecuteOn method runExec.

/**
 * Run the specified Execute object.
 * @param exe the Execute instance representing the external process.
 * @throws BuildException on error
 */
@Override
protected void runExec(Execute exe) throws BuildException {
    int totalFiles = 0;
    int totalDirs = 0;
    boolean haveExecuted = false;
    try {
        Vector<String> fileNames = new Vector<>();
        Vector<File> baseDirs = new Vector<>();
        for (AbstractFileSet fs : filesets) {
            String currentType = type;
            if (fs instanceof DirSet) {
                if (!FileDirBoth.DIR.equals(type)) {
                    log("Found a nested dirset but type is " + type + ". " + "Temporarily switching to type=\"dir\" on the assumption that you really did mean <dirset> not <fileset>.", Project.MSG_DEBUG);
                    currentType = FileDirBoth.DIR;
                }
            }
            File base = fs.getDir(getProject());
            DirectoryScanner ds = fs.getDirectoryScanner(getProject());
            if (!FileDirBoth.DIR.equals(currentType)) {
                for (String value : getFiles(base, ds)) {
                    totalFiles++;
                    fileNames.add(value);
                    baseDirs.add(base);
                }
            }
            if (!FileDirBoth.FILE.equals(currentType)) {
                for (String value : getDirs(base, ds)) {
                    totalDirs++;
                    fileNames.add(value);
                    baseDirs.add(base);
                }
            }
            if (fileNames.isEmpty() && skipEmpty) {
                logSkippingFileset(currentType, ds, base);
                continue;
            }
            if (!parallel) {
                for (String srcFile : fileNames) {
                    String[] command = getCommandline(srcFile, base);
                    log(Commandline.describeCommand(command), Project.MSG_VERBOSE);
                    exe.setCommandline(command);
                    if (redirectorElement != null) {
                        setupRedirector();
                        redirectorElement.configure(redirector, srcFile);
                    }
                    if (redirectorElement != null || haveExecuted) {
                        // need to reset the stream handler to restart
                        // reading of pipes;
                        // go ahead and do it always w/ nested redirectors
                        exe.setStreamHandler(redirector.createHandler());
                    }
                    runExecute(exe);
                    haveExecuted = true;
                }
                fileNames.clear();
                baseDirs.clear();
            }
        }
        if (resources != null) {
            for (Resource res : resources) {
                if (!res.isExists() && ignoreMissing) {
                    continue;
                }
                File base = null;
                String name = res.getName();
                FileProvider fp = res.as(FileProvider.class);
                if (fp != null) {
                    FileResource fr = ResourceUtils.asFileResource(fp);
                    base = fr.getBaseDir();
                    if (base == null) {
                        name = fr.getFile().getAbsolutePath();
                    }
                }
                if (restrict(new String[] { name }, base).length == 0) {
                    continue;
                }
                if ((!res.isDirectory() || !res.isExists()) && !FileDirBoth.DIR.equals(type)) {
                    totalFiles++;
                } else if (res.isDirectory() && !FileDirBoth.FILE.equals(type)) {
                    totalDirs++;
                } else {
                    continue;
                }
                baseDirs.add(base);
                fileNames.add(name);
                if (!parallel) {
                    String[] command = getCommandline(name, base);
                    log(Commandline.describeCommand(command), Project.MSG_VERBOSE);
                    exe.setCommandline(command);
                    if (redirectorElement != null) {
                        setupRedirector();
                        redirectorElement.configure(redirector, name);
                    }
                    if (redirectorElement != null || haveExecuted) {
                        // need to reset the stream handler to restart
                        // reading of pipes;
                        // go ahead and do it always w/ nested redirectors
                        exe.setStreamHandler(redirector.createHandler());
                    }
                    runExecute(exe);
                    haveExecuted = true;
                    fileNames.clear();
                    baseDirs.clear();
                }
            }
        }
        if (parallel && (!fileNames.isEmpty() || !skipEmpty)) {
            runParallel(exe, fileNames, baseDirs);
            haveExecuted = true;
        }
        if (haveExecuted) {
            log("Applied " + cmdl.getExecutable() + " to " + totalFiles + " file" + (totalFiles != 1 ? "s" : "") + " and " + totalDirs + " director" + (totalDirs != 1 ? "ies" : "y") + ".", verbose ? Project.MSG_INFO : Project.MSG_VERBOSE);
        }
    } catch (IOException e) {
        throw new BuildException("Execute failed: " + e, e, getLocation());
    } finally {
        // close the output file if required
        logFlush();
        redirector.setAppendProperties(false);
        redirector.setProperties();
    }
}
Also used : AbstractFileSet(org.apache.tools.ant.types.AbstractFileSet) FileResource(org.apache.tools.ant.types.resources.FileResource) Resource(org.apache.tools.ant.types.Resource) FileResource(org.apache.tools.ant.types.resources.FileResource) IOException(java.io.IOException) DirSet(org.apache.tools.ant.types.DirSet) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) FileProvider(org.apache.tools.ant.types.resources.FileProvider) BuildException(org.apache.tools.ant.BuildException) Vector(java.util.Vector) File(java.io.File)

Example 8 with FileProvider

use of org.apache.tools.ant.types.resources.FileProvider in project ant by apache.

the class Zip method getNonFileSetResourcesToAdd.

/**
 * Collect the resources that are newer than the corresponding
 * entries (or missing) in the original archive.
 *
 * <p>If we are going to recreate the archive instead of updating
 * it, all resources should be considered as new, if a single one
 * is.  Because of this, subclasses overriding this method must
 * call <code>super.getResourcesToAdd</code> and indicate with the
 * third arg if they already know that the archive is
 * out-of-date.</p>
 *
 * @param rcs The filesets to grab resources from
 * @param zipFile intended archive file (may or may not exist)
 * @param needsUpdate whether we already know that the archive is
 * out-of-date.  Subclasses overriding this method are supposed to
 * set this value correctly in their call to
 * <code>super.getResourcesToAdd</code>.
 * @return an array of resources to add for each fileset passed in as well
 *         as a flag that indicates whether the archive is uptodate.
 *
 * @exception BuildException if it likes
 */
protected ArchiveState getNonFileSetResourcesToAdd(final ResourceCollection[] rcs, final File zipFile, boolean needsUpdate) throws BuildException {
    /*
         * Backwards compatibility forces us to repeat the logic of
         * getResourcesToAdd(FileSet[], ...) here once again.
         */
    final Resource[][] initialResources = grabNonFileSetResources(rcs);
    final boolean empty = isEmpty(initialResources);
    HAVE_NON_FILE_SET_RESOURCES_TO_ADD.set(!empty);
    if (empty) {
        // will take care of it.
        return new ArchiveState(needsUpdate, initialResources);
    }
    if (!zipFile.exists()) {
        return new ArchiveState(true, initialResources);
    }
    if (needsUpdate && !doUpdate) {
        // we are recreating the archive, need all resources
        return new ArchiveState(true, initialResources);
    }
    final Resource[][] newerResources = new Resource[rcs.length][];
    for (int i = 0; i < rcs.length; i++) {
        if (initialResources[i].length == 0) {
            newerResources[i] = new Resource[] {};
            continue;
        }
        for (int j = 0; j < initialResources[i].length; j++) {
            final FileProvider fp = initialResources[i][j].as(FileProvider.class);
            if (fp != null && zipFile.equals(fp.getFile())) {
                throw new BuildException("A zip file cannot include itself", getLocation());
            }
        }
        newerResources[i] = selectOutOfDateResources(initialResources[i], new IdentityMapper());
        needsUpdate = needsUpdate || (newerResources[i].length > 0);
        if (needsUpdate && !doUpdate) {
            // to scan further.
            break;
        }
    }
    if (needsUpdate && !doUpdate) {
        // we are recreating the archive, need all resources
        return new ArchiveState(true, initialResources);
    }
    return new ArchiveState(needsUpdate, newerResources);
}
Also used : IdentityMapper(org.apache.tools.ant.util.IdentityMapper) FileProvider(org.apache.tools.ant.types.resources.FileProvider) FileResource(org.apache.tools.ant.types.resources.FileResource) Resource(org.apache.tools.ant.types.Resource) ArchiveResource(org.apache.tools.ant.types.resources.ArchiveResource) ZipResource(org.apache.tools.ant.types.resources.ZipResource) BuildException(org.apache.tools.ant.BuildException)

Example 9 with FileProvider

use of org.apache.tools.ant.types.resources.FileProvider in project ant by apache.

the class Zip method addResources.

/**
 * Add the given resources.
 *
 * @param rc may give additional information like fullpath or
 * permissions.
 * @param resources the resources to add
 * @param zOut the stream to write to
 * @throws IOException on error
 *
 * @since Ant 1.7
 */
protected final void addResources(final ResourceCollection rc, final Resource[] resources, final ZipOutputStream zOut) throws IOException {
    if (rc instanceof FileSet) {
        addResources((FileSet) rc, resources, zOut);
        return;
    }
    for (final Resource resource : resources) {
        String name = resource.getName();
        if (name == null) {
            continue;
        }
        name = name.replace(File.separatorChar, '/');
        if (name.isEmpty()) {
            continue;
        }
        if (resource.isDirectory() && doFilesonly) {
            continue;
        }
        File base = null;
        final FileProvider fp = resource.as(FileProvider.class);
        if (fp != null) {
            base = ResourceUtils.asFileResource(fp).getBaseDir();
        }
        if (resource.isDirectory()) {
            addDirectoryResource(resource, name, "", base, zOut, ArchiveFileSet.DEFAULT_DIR_MODE, ArchiveFileSet.DEFAULT_DIR_MODE);
        } else {
            addParentDirs(base, name, zOut, "", ArchiveFileSet.DEFAULT_DIR_MODE);
            if (fp != null) {
                final File f = (fp).getFile();
                zipFile(f, zOut, name, ArchiveFileSet.DEFAULT_FILE_MODE);
            } else {
                addResource(resource, name, "", zOut, ArchiveFileSet.DEFAULT_FILE_MODE, null, null);
            }
        }
    }
}
Also used : ArchiveFileSet(org.apache.tools.ant.types.ArchiveFileSet) FileSet(org.apache.tools.ant.types.FileSet) ZipFileSet(org.apache.tools.ant.types.ZipFileSet) FileProvider(org.apache.tools.ant.types.resources.FileProvider) FileResource(org.apache.tools.ant.types.resources.FileResource) Resource(org.apache.tools.ant.types.Resource) ArchiveResource(org.apache.tools.ant.types.resources.ArchiveResource) ZipResource(org.apache.tools.ant.types.resources.ZipResource) ZipFile(org.apache.tools.zip.ZipFile) File(java.io.File)

Example 10 with FileProvider

use of org.apache.tools.ant.types.resources.FileProvider in project ant by apache.

the class TraXLiaison method resourceToURI.

private String resourceToURI(final Resource resource) {
    final FileProvider fp = resource.as(FileProvider.class);
    if (fp != null) {
        return FILE_UTILS.toURI(fp.getFile().getAbsolutePath());
    }
    final URLProvider up = resource.as(URLProvider.class);
    if (up != null) {
        final URL u = up.getURL();
        return String.valueOf(u);
    } else {
        return resource.getName();
    }
}
Also used : URLProvider(org.apache.tools.ant.types.resources.URLProvider) FileProvider(org.apache.tools.ant.types.resources.FileProvider) URL(java.net.URL)

Aggregations

FileProvider (org.apache.tools.ant.types.resources.FileProvider)22 BuildException (org.apache.tools.ant.BuildException)13 Resource (org.apache.tools.ant.types.Resource)13 FileResource (org.apache.tools.ant.types.resources.FileResource)12 File (java.io.File)8 IOException (java.io.IOException)4 ArchiveResource (org.apache.tools.ant.types.resources.ArchiveResource)4 ResourceCollection (org.apache.tools.ant.types.ResourceCollection)3 URL (java.net.URL)2 Path (java.nio.file.Path)2 PosixFileAttributeView (java.nio.file.attribute.PosixFileAttributeView)2 ArrayList (java.util.ArrayList)2 Vector (java.util.Vector)2 DirectoryScanner (org.apache.tools.ant.DirectoryScanner)2 FileSet (org.apache.tools.ant.types.FileSet)2 URLProvider (org.apache.tools.ant.types.resources.URLProvider)2 ZipResource (org.apache.tools.ant.types.resources.ZipResource)2 ZipFile (org.apache.tools.zip.ZipFile)2 OverrideOnDemand (com.helger.commons.annotation.OverrideOnDemand)1 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)1