Search in sources :

Example 11 with ResourceCollection

use of org.apache.tools.ant.types.ResourceCollection in project ant by apache.

the class Javadoc method addSourceFiles.

/**
 * Add the files matched by the nested source files to the Vector
 * as SourceFile instances.
 *
 * @since 1.7
 */
private void addSourceFiles(final List<SourceFile> sf) {
    for (ResourceCollection rc : nestedSourceFiles) {
        if (!rc.isFilesystemOnly()) {
            throw new BuildException("only file system based resources are supported by javadoc");
        }
        if (rc instanceof FileSet) {
            final FileSet fs = (FileSet) rc;
            if (!fs.hasPatterns() && !fs.hasSelectors()) {
                final FileSet fs2 = (FileSet) fs.clone();
                fs2.createInclude().setName("**/*.java");
                if (includeNoSourcePackages) {
                    fs2.createInclude().setName("**/package.html");
                }
                rc = fs2;
            }
        }
        for (final Resource r : rc) {
            sf.add(new SourceFile(r.as(FileProvider.class).getFile()));
        }
    }
}
Also used : FileSet(org.apache.tools.ant.types.FileSet) FileProvider(org.apache.tools.ant.types.resources.FileProvider) Resource(org.apache.tools.ant.types.Resource) BuildException(org.apache.tools.ant.BuildException) ResourceCollection(org.apache.tools.ant.types.ResourceCollection)

Example 12 with ResourceCollection

use of org.apache.tools.ant.types.ResourceCollection in project ant by apache.

the class Copy method validateAttributes.

/**
 * Ensure we have a consistent and legal set of attributes, and set
 * any internal flags necessary based on different combinations
 * of attributes.
 * @exception BuildException if an error occurs.
 */
protected void validateAttributes() throws BuildException {
    if (file == null && rcs.isEmpty()) {
        throw new BuildException("Specify at least one source--a file or a resource collection.");
    }
    if (destFile != null && destDir != null) {
        throw new BuildException("Only one of tofile and todir may be set.");
    }
    if (destFile == null && destDir == null) {
        throw new BuildException("One of tofile or todir must be set.");
    }
    if (file != null && file.isDirectory()) {
        throw new BuildException("Use a resource collection to copy directories.");
    }
    if (destFile != null && !rcs.isEmpty()) {
        if (rcs.size() > 1) {
            throw new BuildException("Cannot concatenate multiple files into a single file.");
        }
        final ResourceCollection rc = rcs.elementAt(0);
        if (!rc.isFilesystemOnly() && !supportsNonFileResources()) {
            throw new BuildException("Only FileSystem resources are supported.");
        }
        if (rc.isEmpty()) {
            throw new BuildException(MSG_WHEN_COPYING_EMPTY_RC_TO_FILE);
        }
        if (rc.size() == 1) {
            final Resource res = rc.iterator().next();
            final FileProvider r = res.as(FileProvider.class);
            if (file == null) {
                if (r != null) {
                    file = r.getFile();
                } else {
                    singleResource = res;
                }
                rcs.removeElementAt(0);
            } else {
                throw new BuildException("Cannot concatenate multiple files into a single file.");
            }
        } else {
            throw new BuildException("Cannot concatenate multiple files into a single file.");
        }
    }
    if (destFile != null) {
        destDir = destFile.getParentFile();
    }
}
Also used : FileProvider(org.apache.tools.ant.types.resources.FileProvider) FileResource(org.apache.tools.ant.types.resources.FileResource) Resource(org.apache.tools.ant.types.Resource) BuildException(org.apache.tools.ant.BuildException) ResourceCollection(org.apache.tools.ant.types.ResourceCollection)

Example 13 with ResourceCollection

use of org.apache.tools.ant.types.ResourceCollection in project ant by apache.

the class Delete method execute.

/**
 * Delete the file(s).
 *
 * @exception BuildException if an error occurs
 */
@Override
public void execute() throws BuildException {
    if (usedMatchingTask) {
        log("DEPRECATED - Use of the implicit FileSet is deprecated.  Use a nested fileset element instead.", quiet ? Project.MSG_VERBOSE : verbosity);
    }
    if (file == null && dir == null && filesets.isEmpty() && rcs == null) {
        throw new BuildException("At least one of the file or dir attributes, or a nested resource collection, must be set.");
    }
    if (quiet && failonerror) {
        throw new BuildException("quiet and failonerror cannot both be set to true", getLocation());
    }
    // delete the single file
    if (file != null) {
        if (file.exists()) {
            if (file.isDirectory()) {
                log("Directory " + file.getAbsolutePath() + " cannot be removed using the file attribute.  Use dir instead.", quiet ? Project.MSG_VERBOSE : verbosity);
            } else {
                log("Deleting: " + file.getAbsolutePath());
                if (!delete(file)) {
                    handle("Unable to delete file " + file.getAbsolutePath());
                }
            }
        } else if (isDanglingSymlink(file)) {
            log("Trying to delete file " + file.getAbsolutePath() + " which looks like a broken symlink.", quiet ? Project.MSG_VERBOSE : verbosity);
            if (!delete(file)) {
                handle("Unable to delete file " + file.getAbsolutePath());
            }
        } else {
            log("Could not find file " + file.getAbsolutePath() + " to delete.", quiet ? Project.MSG_VERBOSE : verbosity);
        }
    }
    // delete the directory
    if (dir != null && !usedMatchingTask) {
        if (dir.exists() && dir.isDirectory()) {
            /*
                  If verbosity is MSG_VERBOSE, that mean we are doing
                  regular logging (backwards as that sounds).  In that
                  case, we want to print one message about deleting the
                  top of the directory tree.  Otherwise, the removeDir
                  method will handle messages for _all_ directories.
                */
            if (verbosity == Project.MSG_VERBOSE) {
                log("Deleting directory " + dir.getAbsolutePath());
            }
            removeDir(dir);
        } else if (isDanglingSymlink(dir)) {
            log("Trying to delete directory " + dir.getAbsolutePath() + " which looks like a broken symlink.", quiet ? Project.MSG_VERBOSE : verbosity);
            if (!delete(dir)) {
                handle("Unable to delete directory " + dir.getAbsolutePath());
            }
        }
    }
    Resources resourcesToDelete = new Resources();
    resourcesToDelete.setProject(getProject());
    resourcesToDelete.setCache(true);
    Resources filesetDirs = new Resources();
    filesetDirs.setProject(getProject());
    filesetDirs.setCache(true);
    FileSet implicit = null;
    if (usedMatchingTask && dir != null && dir.isDirectory()) {
        // add the files from the default fileset:
        implicit = getImplicitFileSet();
        implicit.setProject(getProject());
        filesets.add(implicit);
    }
    final int size = filesets.size();
    for (FileSet fs : filesets) {
        if (fs.getProject() == null) {
            log("Deleting fileset with no project specified; assuming executing project", Project.MSG_VERBOSE);
            fs = (FileSet) fs.clone();
            fs.setProject(getProject());
        }
        final File fsDir = fs.getDir();
        if (!fs.getErrorOnMissingDir() && (fsDir == null || !fsDir.exists())) {
            continue;
        }
        if (fsDir == null) {
            throw new BuildException("File or Resource without directory or file specified");
        } else if (!fsDir.isDirectory()) {
            handle("Directory does not exist: " + fsDir);
        } else {
            DirectoryScanner ds = fs.getDirectoryScanner();
            // the previous line has already scanned the
            // filesystem, in order to avoid a rescan when later
            // iterating, capture the results now and store them
            final String[] files = ds.getIncludedFiles();
            resourcesToDelete.add(new ResourceCollection() {

                @Override
                public boolean isFilesystemOnly() {
                    return true;
                }

                @Override
                public int size() {
                    return files.length;
                }

                @Override
                public Iterator<Resource> iterator() {
                    return new FileResourceIterator(getProject(), fsDir, files);
                }
            });
            if (includeEmpty) {
                filesetDirs.add(new ReverseDirs(getProject(), fsDir, ds.getIncludedDirectories()));
            }
            if (removeNotFollowedSymlinks) {
                String[] n = ds.getNotFollowedSymlinks();
                if (n.length > 0) {
                    String[] links = new String[n.length];
                    System.arraycopy(n, 0, links, 0, n.length);
                    Arrays.sort(links, Comparator.reverseOrder());
                    for (String link : links) {
                        final Path filePath = Paths.get(link);
                        if (!Files.isSymbolicLink(filePath)) {
                            // it's not a symbolic link, so move on
                            continue;
                        }
                        // it's a symbolic link, so delete it
                        final boolean deleted = filePath.toFile().delete();
                        if (!deleted) {
                            handle("Could not delete symbolic link at " + filePath);
                        }
                    }
                }
            }
        }
    }
    resourcesToDelete.add(filesetDirs);
    if (rcs != null) {
        // sort first to files, then dirs
        Restrict exists = new Restrict();
        exists.add(EXISTS);
        exists.add(rcs);
        Sort s = new Sort();
        s.add(REVERSE_FILESYSTEM);
        s.add(exists);
        resourcesToDelete.add(s);
    }
    try {
        if (resourcesToDelete.isFilesystemOnly()) {
            for (Resource r : resourcesToDelete) {
                // nonexistent resources could only occur if we already
                // deleted something from a fileset:
                File f = r.as(FileProvider.class).getFile();
                if (!f.exists()) {
                    continue;
                }
                if (!f.isDirectory() || f.list().length == 0) {
                    log("Deleting " + f, verbosity);
                    if (!delete(f) && failonerror) {
                        handle("Unable to delete " + (f.isDirectory() ? "directory " : "file ") + f);
                    }
                }
            }
        } else {
            handle(getTaskName() + " handles only filesystem resources");
        }
    } catch (Exception e) {
        handle(e);
    } finally {
        if (implicit != null) {
            filesets.remove(implicit);
        }
    }
}
Also used : Path(java.nio.file.Path) FileSet(org.apache.tools.ant.types.FileSet) Resource(org.apache.tools.ant.types.Resource) FileResourceIterator(org.apache.tools.ant.types.resources.FileResourceIterator) BuildException(org.apache.tools.ant.BuildException) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) Restrict(org.apache.tools.ant.types.resources.Restrict) FileProvider(org.apache.tools.ant.types.resources.FileProvider) Sort(org.apache.tools.ant.types.resources.Sort) BuildException(org.apache.tools.ant.BuildException) Resources(org.apache.tools.ant.types.resources.Resources) File(java.io.File) ResourceCollection(org.apache.tools.ant.types.ResourceCollection)

Example 14 with ResourceCollection

use of org.apache.tools.ant.types.ResourceCollection in project ant by apache.

the class Concat method execute.

/**
 * Execute the concat task.
 */
@Override
public void execute() {
    validate();
    if (binary && dest == null) {
        throw new BuildException("dest|destfile attribute is required for binary concatenation");
    }
    ResourceCollection c = getResources();
    if (isUpToDate(c)) {
        log(dest + " is up-to-date.", Project.MSG_VERBOSE);
        return;
    }
    if (c.isEmpty() && ignoreEmpty) {
        return;
    }
    try {
        // most of these are defaulted because the concat-as-a-resource code hijacks a lot:
        ResourceUtils.copyResource(new ConcatResource(c), dest == null ? new LogOutputResource(this, Project.MSG_WARN) : dest, null, null, true, false, append, null, null, getProject(), force);
    } catch (IOException e) {
        throw new BuildException("error concatenating content to " + dest, e);
    }
}
Also used : BuildException(org.apache.tools.ant.BuildException) IOException(java.io.IOException) LogOutputResource(org.apache.tools.ant.types.resources.LogOutputResource) ResourceCollection(org.apache.tools.ant.types.ResourceCollection)

Example 15 with ResourceCollection

use of org.apache.tools.ant.types.ResourceCollection in project ant by apache.

the class Last method getCollection.

/**
 * Take the last <code>count</code> elements.
 * @return a Collection of Resources.
 */
@Override
protected Collection<Resource> getCollection() {
    int count = getValidCount();
    ResourceCollection rc = getResourceCollection();
    int size = rc.size();
    int skip = Math.max(0, size - count);
    List<Resource> result = rc.stream().skip(skip).collect(Collectors.toList());
    int found = result.size();
    if (found == count || (size < count && found == size)) {
        return result;
    }
    // mismatch:
    String msg = String.format("Resource collection %s reports size %d but returns %d elements.", rc, size, found + skip);
    // size was understated -> too many results; warn and continue:
    if (found > count) {
        log(msg, Project.MSG_WARN);
        return result.subList(found - count, found);
    }
    // size was overstated; we missed some and are now in error-land:
    throw new BuildException(msg);
}
Also used : Resource(org.apache.tools.ant.types.Resource) BuildException(org.apache.tools.ant.BuildException) ResourceCollection(org.apache.tools.ant.types.ResourceCollection)

Aggregations

ResourceCollection (org.apache.tools.ant.types.ResourceCollection)21 Resource (org.apache.tools.ant.types.Resource)16 BuildException (org.apache.tools.ant.BuildException)15 ArrayList (java.util.ArrayList)9 FileResource (org.apache.tools.ant.types.resources.FileResource)8 File (java.io.File)7 IOException (java.io.IOException)6 FileSet (org.apache.tools.ant.types.FileSet)6 FileProvider (org.apache.tools.ant.types.resources.FileProvider)5 DirectoryScanner (org.apache.tools.ant.DirectoryScanner)4 ZipResource (org.apache.tools.ant.types.resources.ZipResource)4 ArchiveResource (org.apache.tools.ant.types.resources.ArchiveResource)3 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Union (org.apache.tools.ant.types.resources.Union)2 ZipFile (org.apache.tools.zip.ZipFile)2 OverrideOnDemand (com.helger.commons.annotation.OverrideOnDemand)1 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)1 CommonsHashMap (com.helger.commons.collection.impl.CommonsHashMap)1