Search in sources :

Example 36 with FileSet

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

the class Zip method getResourcesToAdd.

/**
 * 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>
 *
 * <p>This method first delegates to getNonFileSetResourcesToAdd
 * and then invokes the FileSet-arg version.  All this to keep
 * backwards compatibility for subclasses that don't know how to
 * deal with non-FileSet ResourceCollections.</p>
 *
 * @param rcs The resource collections 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
 * @since Ant 1.7
 */
protected ArchiveState getResourcesToAdd(final ResourceCollection[] rcs, final File zipFile, final boolean needsUpdate) throws BuildException {
    final List<FileSet> filesets = new ArrayList<>();
    final List<ResourceCollection> rest = new ArrayList<>();
    for (ResourceCollection rc : rcs) {
        if (rc instanceof FileSet) {
            filesets.add((FileSet) rc);
        } else {
            rest.add(rc);
        }
    }
    final ResourceCollection[] rc = rest.toArray(new ResourceCollection[rest.size()]);
    ArchiveState as = getNonFileSetResourcesToAdd(rc, zipFile, needsUpdate);
    final FileSet[] fs = filesets.toArray(new FileSet[filesets.size()]);
    final ArchiveState as2 = getResourcesToAdd(fs, zipFile, as.isOutOfDate());
    if (!as.isOutOfDate() && as2.isOutOfDate()) {
        /*
             * Bad luck.
             *
             * There are resources in the filesets that make the
             * archive out of date, but not in the non-fileset
             * resources. We need to rescan the non-FileSets to grab
             * all of them now.
             */
        as = getNonFileSetResourcesToAdd(rc, zipFile, true);
    }
    final Resource[][] toAdd = new Resource[rcs.length][];
    int fsIndex = 0;
    int restIndex = 0;
    for (int i = 0; i < rcs.length; i++) {
        if (rcs[i] instanceof FileSet) {
            toAdd[i] = as2.getResourcesToAdd()[fsIndex++];
        } else {
            toAdd[i] = as.getResourcesToAdd()[restIndex++];
        }
    }
    return new ArchiveState(as2.isOutOfDate(), toAdd);
}
Also used : ArchiveFileSet(org.apache.tools.ant.types.ArchiveFileSet) FileSet(org.apache.tools.ant.types.FileSet) ZipFileSet(org.apache.tools.ant.types.ZipFileSet) ArrayList(java.util.ArrayList) 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) ResourceCollection(org.apache.tools.ant.types.ResourceCollection)

Example 37 with FileSet

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

the class Translate method translate.

/**
 * Reads source file line by line using the source encoding and
 * searches for keys that are sandwiched between the startToken
 * and endToken.  The values for these keys are looked up from
 * the hashtable and substituted.  If the hashtable doesn't
 * contain the key, they key itself is used as the value.
 * Destination files and directories are created as needed.
 * The destination file is overwritten only if
 * the forceoverwritten attribute is set to true if
 * the source file or any associated bundle resource file is
 * newer than the destination file.
 */
private void translate() throws BuildException {
    int filesProcessed = 0;
    for (FileSet fs : filesets) {
        DirectoryScanner ds = fs.getDirectoryScanner(getProject());
        for (String srcFile : ds.getIncludedFiles()) {
            try {
                File dest = FILE_UTILS.resolveFile(toDir, srcFile);
                // Make sure parent dirs exist, else, create them.
                try {
                    File destDir = new File(dest.getParent());
                    if (!destDir.exists()) {
                        destDir.mkdirs();
                    }
                } catch (Exception e) {
                    log("Exception occurred while trying to check/create " + " parent directory.  " + e.getMessage(), Project.MSG_DEBUG);
                }
                destLastModified = dest.lastModified();
                File src = FILE_UTILS.resolveFile(ds.getBasedir(), srcFile);
                srcLastModified = src.lastModified();
                // Check to see if dest file has to be recreated
                boolean needsWork = forceOverwrite || destLastModified < srcLastModified;
                if (!needsWork) {
                    for (int icounter = 0; icounter < BUNDLE_MAX_ALTERNATIVES; icounter++) {
                        needsWork = (destLastModified < bundleLastModified[icounter]);
                        if (needsWork) {
                            break;
                        }
                    }
                }
                if (needsWork) {
                    log("Processing " + srcFile, Project.MSG_DEBUG);
                    translateOneFile(src, dest);
                    ++filesProcessed;
                } else {
                    log("Skipping " + srcFile + " as destination file is up to date", Project.MSG_VERBOSE);
                }
            } catch (IOException ioe) {
                throw new BuildException(ioe.getMessage(), getLocation());
            }
        }
    }
    log("Translation performed on " + filesProcessed + " file(s).", Project.MSG_DEBUG);
}
Also used : FileSet(org.apache.tools.ant.types.FileSet) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) File(java.io.File) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException)

Example 38 with FileSet

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

the class EjbJar method createSupport.

/**
 * Adds a fileset for support elements.
 *
 * @return a fileset which can be populated with support files.
 */
public FileSet createSupport() {
    FileSet supportFileSet = new FileSet();
    config.supportFileSets.add(supportFileSet);
    return supportFileSet;
}
Also used : FileSet(org.apache.tools.ant.types.FileSet)

Example 39 with FileSet

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

the class GenericDeploymentTool method addSupportClasses.

/**
 * Adds any classes the user specifies using <i>support</i> nested elements
 * to the <code>ejbFiles</code> Hashtable.
 *
 * @param ejbFiles Hashtable of EJB classes (and other) files that will be
 *                 added to the completed JAR file
 */
protected void addSupportClasses(Hashtable<String, File> ejbFiles) {
    // add in support classes if any
    Project project = task.getProject();
    for (FileSet supportFileSet : config.supportFileSets) {
        File supportBaseDir = supportFileSet.getDir(project);
        DirectoryScanner supportScanner = supportFileSet.getDirectoryScanner(project);
        for (String supportFile : supportScanner.getIncludedFiles()) {
            ejbFiles.put(supportFile, new File(supportBaseDir, supportFile));
        }
    }
}
Also used : Project(org.apache.tools.ant.Project) FileSet(org.apache.tools.ant.types.FileSet) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) File(java.io.File)

Example 40 with FileSet

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

the class Sync method removeOrphanFiles.

/**
 * Removes all files and folders not found as keys of a table
 * (used as a set!).
 *
 * <p>If the provided file is a directory, it is recursively
 * scanned for orphaned files which will be removed as well.</p>
 *
 * <p>If the directory is an orphan, it will also be removed.</p>
 *
 * @param  nonOrphans the table of all non-orphan <code>File</code>s.
 * @param  toDir the initial file or directory to scan or test.
 * @param  preservedDirectories will be filled with the directories
 *         matched by preserveInTarget - if any.  Will not be
 *         filled unless preserveEmptyDirs and includeEmptyDirs
 *         conflict.
 * @return the number of orphaned files and directories actually removed.
 * Position 0 of the array is the number of orphaned directories.
 * Position 1 of the array is the number or orphaned files.
 */
private int[] removeOrphanFiles(Set<String> nonOrphans, File toDir, Set<File> preservedDirectories) {
    int[] removedCount = new int[] { 0, 0 };
    String[] excls = nonOrphans.toArray(new String[nonOrphans.size() + 1]);
    // want to keep toDir itself
    excls[nonOrphans.size()] = "";
    DirectoryScanner ds;
    if (syncTarget != null) {
        FileSet fs = syncTarget.toFileSet(false);
        fs.setDir(toDir);
        // preserveInTarget would find all files we want to keep,
        // but we need to find all that we want to delete - so the
        // meaning of all patterns and selectors must be inverted
        PatternSet ps = syncTarget.mergePatterns(getProject());
        fs.appendExcludes(ps.getIncludePatterns(getProject()));
        fs.appendIncludes(ps.getExcludePatterns(getProject()));
        fs.setDefaultexcludes(!syncTarget.getDefaultexcludes());
        // selectors are implicitly ANDed in DirectoryScanner.  To
        // revert their logic we wrap them into a <none> selector
        // instead.
        FileSelector[] s = syncTarget.getSelectors(getProject());
        if (s.length > 0) {
            NoneSelector ns = new NoneSelector();
            for (FileSelector element : s) {
                ns.appendSelector(element);
            }
            fs.appendSelector(ns);
        }
        ds = fs.getDirectoryScanner(getProject());
    } else {
        ds = new DirectoryScanner();
        ds.setBasedir(toDir);
    }
    ds.addExcludes(excls);
    ds.scan();
    for (String file : ds.getIncludedFiles()) {
        File f = new File(toDir, file);
        log("Removing orphan file: " + f, Project.MSG_DEBUG);
        f.delete();
        ++removedCount[1];
    }
    String[] dirs = ds.getIncludedDirectories();
    // ds returns the directories in lexicographic order.
    // iterating through the array backwards means we are deleting
    // leaves before their parent nodes - thus making sure (well,
    // more likely) that the directories are empty when we try to
    // delete them.
    Arrays.sort(dirs, Comparator.reverseOrder());
    for (String dir : dirs) {
        File f = new File(toDir, dir);
        String[] children = f.list();
        if (children == null || children.length < 1) {
            log("Removing orphan directory: " + f, Project.MSG_DEBUG);
            f.delete();
            ++removedCount[0];
        }
    }
    Boolean ped = getExplicitPreserveEmptyDirs();
    if (ped != null && ped != myCopy.getIncludeEmptyDirs()) {
        FileSet fs = syncTarget.toFileSet(true);
        fs.setDir(toDir);
        String[] preservedDirs = fs.getDirectoryScanner(getProject()).getIncludedDirectories();
        Arrays.sort(preservedDirs, Comparator.reverseOrder());
        for (String dir : preservedDirs) {
            preservedDirectories.add(new File(toDir, dir));
        }
    }
    return removedCount;
}
Also used : AbstractFileSet(org.apache.tools.ant.types.AbstractFileSet) FileSet(org.apache.tools.ant.types.FileSet) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) FileSelector(org.apache.tools.ant.types.selectors.FileSelector) PatternSet(org.apache.tools.ant.types.PatternSet) File(java.io.File) NoneSelector(org.apache.tools.ant.types.selectors.NoneSelector)

Aggregations

FileSet (org.apache.tools.ant.types.FileSet)165 File (java.io.File)124 DirectoryScanner (org.apache.tools.ant.DirectoryScanner)83 BuildException (org.apache.tools.ant.BuildException)49 Test (org.junit.Test)41 IOException (java.io.IOException)36 ArrayList (java.util.ArrayList)29 Project (org.apache.tools.ant.Project)22 Resource (org.apache.tools.ant.types.Resource)12 FileResource (org.apache.tools.ant.types.resources.FileResource)10 URL (java.net.URL)6 Hashtable (java.util.Hashtable)6 ArchiveFileSet (org.apache.tools.ant.types.ArchiveFileSet)6 Path (org.apache.tools.ant.types.Path)6 ResourceCollection (org.apache.tools.ant.types.ResourceCollection)6 PrintStream (java.io.PrintStream)5 HashMap (java.util.HashMap)5 Iterator (java.util.Iterator)5 List (java.util.List)5 ZipFileSet (org.apache.tools.ant.types.ZipFileSet)5