Search in sources :

Example 6 with ZipFileSet

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

the class Zip method processGroupFilesets.

/**
 * Process groupfilesets
 */
private void processGroupFilesets() {
    // Add the files found in groupfileset to fileset
    for (FileSet fs : groupfilesets) {
        logWhenWriting("Processing groupfileset ", Project.MSG_VERBOSE);
        final FileScanner scanner = fs.getDirectoryScanner(getProject());
        final File basedir = scanner.getBasedir();
        for (String file : scanner.getIncludedFiles()) {
            logWhenWriting("Adding file " + file + " to fileset", Project.MSG_VERBOSE);
            final ZipFileSet zf = new ZipFileSet();
            zf.setProject(getProject());
            zf.setSrc(new File(basedir, file));
            add(zf);
            filesetsFromGroupfilesets.add(zf);
        }
    }
}
Also used : ArchiveFileSet(org.apache.tools.ant.types.ArchiveFileSet) FileSet(org.apache.tools.ant.types.FileSet) ZipFileSet(org.apache.tools.ant.types.ZipFileSet) ZipFileSet(org.apache.tools.ant.types.ZipFileSet) ZipFile(org.apache.tools.zip.ZipFile) File(java.io.File) FileScanner(org.apache.tools.ant.FileScanner)

Example 7 with ZipFileSet

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

the class Zip method executeMain.

/**
 * Build the zip file.
 * This is called twice if doubleFilePass is true.
 * @throws BuildException on error
 */
public void executeMain() throws BuildException {
    checkAttributesAndElements();
    // Renamed version of original file, if it exists
    File renamedFile = null;
    addingNewFiles = true;
    processDoUpdate();
    processGroupFilesets();
    // collect filesets to pass them to getResourcesToAdd
    final List<ResourceCollection> vfss = new ArrayList<>();
    if (baseDir != null) {
        final FileSet fs = (FileSet) getImplicitFileSet().clone();
        fs.setDir(baseDir);
        vfss.add(fs);
    }
    vfss.addAll(resources);
    final ResourceCollection[] fss = vfss.toArray(new ResourceCollection[vfss.size()]);
    boolean success = false;
    try {
        // can also handle empty archives
        final ArchiveState state = getResourcesToAdd(fss, zipFile, false);
        // quick exit if the target is up to date
        if (!state.isOutOfDate()) {
            return;
        }
        final File parent = zipFile.getParentFile();
        if (parent != null && !parent.isDirectory() && !(parent.mkdirs() || parent.isDirectory())) {
            throw new BuildException("Failed to create missing parent directory for %s", zipFile);
        }
        updatedFile = true;
        if (!zipFile.exists() && state.isWithoutAnyResources()) {
            createEmptyZip(zipFile);
            return;
        }
        final Resource[][] addThem = state.getResourcesToAdd();
        if (doUpdate) {
            renamedFile = renameFile();
        }
        final String action = doUpdate ? "Updating " : "Building ";
        if (!skipWriting) {
            log(action + archiveType + ": " + zipFile.getAbsolutePath());
        }
        ZipOutputStream zOut = null;
        try {
            if (!skipWriting) {
                zOut = new ZipOutputStream(zipFile);
                zOut.setEncoding(encoding);
                zOut.setUseLanguageEncodingFlag(useLanguageEncodingFlag);
                zOut.setCreateUnicodeExtraFields(createUnicodeExtraFields.getPolicy());
                zOut.setFallbackToUTF8(fallBackToUTF8);
                zOut.setMethod(doCompress ? ZipOutputStream.DEFLATED : ZipOutputStream.STORED);
                zOut.setLevel(level);
                zOut.setUseZip64(zip64Mode.getMode());
            }
            initZipOutputStream(zOut);
            // Add the explicit resource collections to the archive.
            for (int i = 0; i < fss.length; i++) {
                if (addThem[i].length != 0) {
                    addResources(fss[i], addThem[i], zOut);
                }
            }
            if (doUpdate) {
                addingNewFiles = false;
                final ZipFileSet oldFiles = new ZipFileSet();
                oldFiles.setProject(getProject());
                oldFiles.setSrc(renamedFile);
                oldFiles.setDefaultexcludes(false);
                for (String addedFile : addedFiles) {
                    oldFiles.createExclude().setName(addedFile);
                }
                final DirectoryScanner ds = oldFiles.getDirectoryScanner(getProject());
                ((ZipScanner) ds).setEncoding(encoding);
                Stream<String> includedResourceNames = Stream.of(ds.getIncludedFiles());
                if (!doFilesonly) {
                    includedResourceNames = Stream.concat(includedResourceNames, Stream.of(ds.getIncludedDirectories()));
                }
                Resource[] r = includedResourceNames.map(ds::getResource).toArray(Resource[]::new);
                addResources(oldFiles, r, zOut);
            }
            if (zOut != null) {
                zOut.setComment(comment);
            }
            finalizeZipOutputStream(zOut);
            // temporary file
            if (doUpdate) {
                if (!renamedFile.delete()) {
                    log("Warning: unable to delete temporary file " + renamedFile.getName(), Project.MSG_WARN);
                }
            }
            success = true;
        } finally {
            // Close the output stream.
            closeZout(zOut, success);
        }
    } catch (final IOException ioe) {
        String msg = "Problem creating " + archiveType + ": " + ioe.getMessage();
        // delete a bogus ZIP file (but only if it's not the original one)
        if ((!doUpdate || renamedFile != null) && !zipFile.delete()) {
            msg += " (and the archive is probably corrupt but I could not " + "delete it)";
        }
        if (doUpdate && renamedFile != null) {
            try {
                FILE_UTILS.rename(renamedFile, zipFile);
            } catch (final IOException e) {
                msg += " (and I couldn't rename the temporary file " + renamedFile.getName() + " back)";
            }
        }
        throw new BuildException(msg, ioe, getLocation());
    } finally {
        cleanUp();
    }
}
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) IOException(java.io.IOException) ZipFileSet(org.apache.tools.ant.types.ZipFileSet) ZipOutputStream(org.apache.tools.zip.ZipOutputStream) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) BuildException(org.apache.tools.ant.BuildException) ZipScanner(org.apache.tools.ant.types.ZipScanner) ZipFile(org.apache.tools.zip.ZipFile) File(java.io.File) ResourceCollection(org.apache.tools.ant.types.ResourceCollection)

Example 8 with ZipFileSet

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

the class War method setWebxml.

/**
 * set the deployment descriptor to use (WEB-INF/web.xml);
 * required unless <tt>update=true</tt>
 * @param descr the deployment descriptor file
 */
public void setWebxml(File descr) {
    deploymentDescriptor = descr;
    if (!deploymentDescriptor.exists()) {
        throw new BuildException("Deployment descriptor:  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 9 with ZipFileSet

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

the class CompileJSPs method updateSourceWar.

private void updateSourceWar(File jspCompileDir) {
    // Finally need to merge the compiled jsps in
    War warTask = new War();
    warTask.setProject(getProject());
    warTask.setDestFile(war);
    warTask.setUpdate(true);
    ZipFileSet jspFiles = new ZipFileSet();
    // The JSPs will be in the a well known location. The
    // app name from server.xml and the war file name will
    // be
    // in the path, the war name minus the .war extension
    // (if present) will also be used.
    jspFiles.setDir(jspCompileDir);
    warTask.addClasses(jspFiles);
    warTask.setTaskName(getTaskName());
    warTask.execute();
}
Also used : War(org.apache.tools.ant.taskdefs.War) ZipFileSet(org.apache.tools.ant.types.ZipFileSet)

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