Search in sources :

Example 1 with ZipFileSet

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

the class Ear method setAppxml.

/**
 * File to incorporate as application.xml.
 * @param descr the descriptor file
 */
public void setAppxml(File descr) {
    deploymentDescriptor = descr;
    if (!deploymentDescriptor.exists()) {
        throw new BuildException("Deployment descriptor: %s does not exist.", deploymentDescriptor);
    }
    // Create a ZipFileSet for this file, and pass it up.
    ZipFileSet fs = new ZipFileSet();
    fs.setFile(deploymentDescriptor);
    fs.setFullpath(XML_DESCRIPTOR_PATH);
    super.addFileset(fs);
}
Also used : BuildException(org.apache.tools.ant.BuildException) ZipFileSet(org.apache.tools.ant.types.ZipFileSet)

Example 2 with ZipFileSet

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

the class Zip method grabResources.

/**
 * Fetch all included and not excluded resources from the sets.
 *
 * <p>Included directories will precede included files.</p>
 * @param filesets an array of filesets
 * @return the resources included
 * @since Ant 1.5.2
 */
protected Resource[][] grabResources(final FileSet[] filesets) {
    final Resource[][] result = new Resource[filesets.length][];
    for (int i = 0; i < filesets.length; i++) {
        boolean skipEmptyNames = true;
        if (filesets[i] instanceof ZipFileSet) {
            final ZipFileSet zfs = (ZipFileSet) filesets[i];
            skipEmptyNames = zfs.getPrefix(getProject()).isEmpty() && zfs.getFullpath(getProject()).isEmpty();
        }
        final DirectoryScanner rs = filesets[i].getDirectoryScanner(getProject());
        if (rs instanceof ZipScanner) {
            ((ZipScanner) rs).setEncoding(encoding);
        }
        final List<Resource> resources = new Vector<>();
        if (!doFilesonly) {
            for (String d : rs.getIncludedDirectories()) {
                if (!d.isEmpty() || !skipEmptyNames) {
                    resources.add(rs.getResource(d));
                }
            }
        }
        for (String f : rs.getIncludedFiles()) {
            if (!f.isEmpty() || !skipEmptyNames) {
                resources.add(rs.getResource(f));
            }
        }
        result[i] = resources.toArray(new Resource[resources.size()]);
    }
    return result;
}
Also used : DirectoryScanner(org.apache.tools.ant.DirectoryScanner) 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) ZipScanner(org.apache.tools.ant.types.ZipScanner) ZipFileSet(org.apache.tools.ant.types.ZipFileSet) Vector(java.util.Vector)

Example 3 with ZipFileSet

use of org.apache.tools.ant.types.ZipFileSet 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>
 *
 * @param filesets 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 getResourcesToAdd(final FileSet[] filesets, final File zipFile, boolean needsUpdate) throws BuildException {
    final Resource[][] initialResources = grabResources(filesets);
    if (isEmpty(initialResources)) {
        if (Boolean.FALSE.equals(HAVE_NON_FILE_SET_RESOURCES_TO_ADD.get())) {
            if (needsUpdate && doUpdate) {
                /*
                     * This is a rather hairy case.
                     *
                     * One of our subclasses knows that we need to
                     * update the archive, but at the same time, there
                     * are no resources known to us that would need to
                     * be added.  Only the subclass seems to know
                     * what's going on.
                     *
                     * This happens if <jar> detects that the manifest
                     * has changed, for example.  The manifest is not
                     * part of any resources because of our support
                     * for inline <manifest>s.
                     *
                     * If we invoke createEmptyZip like Ant 1.5.2 did,
                     * we'll loose all stuff that has been in the
                     * original archive (bugzilla report 17780).
                     */
                return new ArchiveState(true, initialResources);
            }
            if ("skip".equals(emptyBehavior)) {
                if (doUpdate) {
                    logWhenWriting(archiveType + " archive " + zipFile + " not updated because no new files were" + " included.", Project.MSG_VERBOSE);
                } else {
                    logWhenWriting("Warning: skipping " + archiveType + " archive " + zipFile + " because no files were included.", Project.MSG_WARN);
                }
            } else if ("fail".equals(emptyBehavior)) {
                throw new BuildException("Cannot create " + archiveType + " archive " + zipFile + ": no files were included.", getLocation());
            } else {
                // Create.
                if (!zipFile.exists()) {
                    needsUpdate = true;
                }
            }
        }
        // (re-)create the archive anyway
        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[filesets.length][];
    for (int i = 0; i < filesets.length; i++) {
        if (!(fileset instanceof ZipFileSet) || ((ZipFileSet) fileset).getSrc(getProject()) == null) {
            final File base = filesets[i].getDir(getProject());
            for (int j = 0; j < initialResources[i].length; j++) {
                final File resourceAsFile = FILE_UTILS.resolveFile(base, initialResources[i][j].getName());
                if (resourceAsFile.equals(zipFile)) {
                    throw new BuildException("A zip file cannot include " + "itself", getLocation());
                }
            }
        }
    }
    for (int i = 0; i < filesets.length; i++) {
        if (initialResources[i].length == 0) {
            newerResources[i] = new Resource[] {};
            continue;
        }
        FileNameMapper myMapper = new IdentityMapper();
        if (filesets[i] instanceof ZipFileSet) {
            final ZipFileSet zfs = (ZipFileSet) filesets[i];
            if (zfs.getFullpath(getProject()) != null && !zfs.getFullpath(getProject()).equals("")) {
                // in this case all files from origin map to
                // the fullPath attribute of the zipfileset at
                // destination
                final MergingMapper fm = new MergingMapper();
                fm.setTo(zfs.getFullpath(getProject()));
                myMapper = fm;
            } else if (zfs.getPrefix(getProject()) != null && !zfs.getPrefix(getProject()).equals("")) {
                final GlobPatternMapper gm = new GlobPatternMapper();
                gm.setFrom("*");
                String prefix = zfs.getPrefix(getProject());
                if (!prefix.endsWith("/") && !prefix.endsWith("\\")) {
                    prefix += "/";
                }
                gm.setTo(prefix + "*");
                myMapper = gm;
            }
        }
        newerResources[i] = selectOutOfDateResources(initialResources[i], myMapper);
        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) GlobPatternMapper(org.apache.tools.ant.util.GlobPatternMapper) 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) FileNameMapper(org.apache.tools.ant.util.FileNameMapper) ZipFileSet(org.apache.tools.ant.types.ZipFileSet) ZipFile(org.apache.tools.zip.ZipFile) File(java.io.File) MergingMapper(org.apache.tools.ant.util.MergingMapper)

Example 4 with ZipFileSet

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

the class Zip method addResources.

/**
 * Add the given resources.
 *
 * @param fileset 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.5.2
 */
protected final void addResources(final FileSet fileset, final Resource[] resources, final ZipOutputStream zOut) throws IOException {
    String prefix = "";
    String fullpath = "";
    int dirMode = ArchiveFileSet.DEFAULT_DIR_MODE;
    int fileMode = ArchiveFileSet.DEFAULT_FILE_MODE;
    ArchiveFileSet zfs = null;
    if (fileset instanceof ArchiveFileSet) {
        zfs = (ArchiveFileSet) fileset;
        prefix = zfs.getPrefix(getProject());
        fullpath = zfs.getFullpath(getProject());
        dirMode = zfs.getDirMode(getProject());
        fileMode = zfs.getFileMode(getProject());
    }
    if (prefix.length() > 0 && fullpath.length() > 0) {
        throw new BuildException("Both prefix and fullpath attributes must not be set on the same fileset.");
    }
    if (resources.length != 1 && fullpath.length() > 0) {
        throw new BuildException("fullpath attribute may only be specified for filesets that specify a single file.");
    }
    if (!prefix.isEmpty()) {
        if (!prefix.endsWith("/") && !prefix.endsWith("\\")) {
            prefix += "/";
        }
        addParentDirs(null, prefix, zOut, "", dirMode);
    }
    ZipFile zf = null;
    try {
        boolean dealingWithFiles = false;
        File base = null;
        if (zfs == null || zfs.getSrc(getProject()) == null) {
            dealingWithFiles = true;
            base = fileset.getDir(getProject());
        } else if (zfs instanceof ZipFileSet) {
            zf = new ZipFile(zfs.getSrc(getProject()), encoding);
        }
        for (Resource resource : resources) {
            String name;
            if (fullpath.isEmpty()) {
                name = resource.getName();
            } else {
                name = fullpath;
            }
            name = name.replace(File.separatorChar, '/');
            if (name.isEmpty()) {
                continue;
            }
            if (resource.isDirectory()) {
                if (doFilesonly) {
                    continue;
                }
                final int thisDirMode = zfs != null && zfs.hasDirModeBeenSet() ? dirMode : getUnixMode(resource, zf, dirMode);
                addDirectoryResource(resource, name, prefix, base, zOut, dirMode, thisDirMode);
            } else {
                // !isDirectory
                addParentDirs(base, name, zOut, prefix, dirMode);
                if (dealingWithFiles) {
                    final File f = FILE_UTILS.resolveFile(base, resource.getName());
                    zipFile(f, zOut, prefix + name, fileMode);
                } else {
                    final int thisFileMode = zfs != null && zfs.hasFileModeBeenSet() ? fileMode : getUnixMode(resource, zf, fileMode);
                    addResource(resource, name, prefix, zOut, thisFileMode, zf, zfs == null ? null : zfs.getSrc(getProject()));
                }
            }
        }
    } finally {
        if (zf != null) {
            zf.close();
        }
    }
}
Also used : ZipFile(org.apache.tools.zip.ZipFile) 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) ArchiveFileSet(org.apache.tools.ant.types.ArchiveFileSet) ZipFileSet(org.apache.tools.ant.types.ZipFileSet) ZipFile(org.apache.tools.zip.ZipFile) File(java.io.File)

Example 5 with ZipFileSet

use of org.apache.tools.ant.types.ZipFileSet in project processdash by dtuma.

the class MergeJars method copyFileSet.

private void copyFileSet(FileSet fileset, ZipOutputStream out) throws IOException {
    String prefix = null;
    String fullPath = null;
    if (fileset instanceof ZipFileSet) {
        ZipFileSet zfs = (ZipFileSet) fileset;
        fullPath = zfs.getFullpath(getProject());
        prefix = zfs.getPrefix(getProject());
        if (prefix != null && !prefix.endsWith("/"))
            prefix = prefix + "/";
    }
    DirectoryScanner ds = fileset.getDirectoryScanner(getProject());
    String[] srcFiles = ds.getIncludedFiles();
    if (fullPath != null && srcFiles.length > 1)
        throw new BuildException("fullpath specified for fileset matching multiple files");
    File dir = fileset.getDir(getProject());
    for (int i = 0; i < srcFiles.length; i++) {
        String filename = srcFiles[i];
        File inputFile = new File(dir, filename);
        FileInputStream in = new FileInputStream(inputFile);
        filename = filename.replace(File.separatorChar, '/');
        if (fullPath != null)
            filename = fullPath;
        else if (prefix != null)
            filename = prefix + filename;
        ZipEntry zipEntry = new ZipEntry(filename);
        zipEntry.setTime(inputFile.lastModified());
        copyFile(zipEntry, out, in);
    }
}
Also used : DirectoryScanner(org.apache.tools.ant.DirectoryScanner) ZipEntry(java.util.zip.ZipEntry) BuildException(org.apache.tools.ant.BuildException) ZipFileSet(org.apache.tools.ant.types.ZipFileSet) JarFile(java.util.jar.JarFile) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

ZipFileSet (org.apache.tools.ant.types.ZipFileSet)9 BuildException (org.apache.tools.ant.BuildException)6 File (java.io.File)5 Resource (org.apache.tools.ant.types.Resource)4 ArchiveResource (org.apache.tools.ant.types.resources.ArchiveResource)4 FileResource (org.apache.tools.ant.types.resources.FileResource)4 ZipResource (org.apache.tools.ant.types.resources.ZipResource)4 ZipFile (org.apache.tools.zip.ZipFile)4 DirectoryScanner (org.apache.tools.ant.DirectoryScanner)3 ArchiveFileSet (org.apache.tools.ant.types.ArchiveFileSet)3 FileSet (org.apache.tools.ant.types.FileSet)2 ZipScanner (org.apache.tools.ant.types.ZipScanner)2 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Vector (java.util.Vector)1 JarFile (java.util.jar.JarFile)1 ZipEntry (java.util.zip.ZipEntry)1 FileScanner (org.apache.tools.ant.FileScanner)1 War (org.apache.tools.ant.taskdefs.War)1