Search in sources :

Example 1 with ZipUnArchiver

use of org.codehaus.plexus.archiver.zip.ZipUnArchiver in project tycho by eclipse.

the class UpdateSiteAssembler method unpackJar.

private void unpackJar(File location, File outputJar) {
    ZipUnArchiver unzip;
    FileLockService fileLockService;
    try {
        unzip = (ZipUnArchiver) session.lookup(ZipUnArchiver.ROLE, "zip");
        fileLockService = (FileLockService) session.lookup(FileLockService.class.getName());
    } catch (ComponentLookupException e) {
        throw new RuntimeException("Could not lookup required component", e);
    }
    outputJar.mkdirs();
    if (!outputJar.isDirectory()) {
        throw new RuntimeException("Could not create output directory " + outputJar.getAbsolutePath());
    }
    unzip.setSourceFile(location);
    unzip.setDestDirectory(outputJar);
    FileLocker locker = fileLockService.getFileLocker(location);
    locker.lock();
    try {
        unzip.extract();
    } catch (ArchiverException e) {
        throw new RuntimeException("Could not unpack jar", e);
    } finally {
        locker.release();
    }
}
Also used : FileLocker(org.eclipse.tycho.locking.facade.FileLocker) ArchiverException(org.codehaus.plexus.archiver.ArchiverException) ZipUnArchiver(org.codehaus.plexus.archiver.zip.ZipUnArchiver) ComponentLookupException(org.codehaus.plexus.component.repository.exception.ComponentLookupException) FileLockService(org.eclipse.tycho.locking.facade.FileLockService)

Example 2 with ZipUnArchiver

use of org.codehaus.plexus.archiver.zip.ZipUnArchiver in project maven-dependency-plugin by apache.

the class AbstractDependencyMojo method unpack.

/**
 * @param artifact {@link Artifact}
 * @param type The type.
 * @param location The location.
 * @param includes includes list.
 * @param excludes excludes list.
 * @param encoding the encoding.
 * @throws MojoExecutionException in case of an error.
 */
protected void unpack(Artifact artifact, String type, File location, String includes, String excludes, String encoding) throws MojoExecutionException {
    File file = artifact.getFile();
    try {
        logUnpack(file, location, includes, excludes);
        location.mkdirs();
        if (!location.exists()) {
            throw new MojoExecutionException("Location to write unpacked files to could not be created: " + location);
        }
        if (file.isDirectory()) {
            // usual case is a future jar packaging, but there are special cases: classifier and other packaging
            throw new MojoExecutionException("Artifact has not been packaged yet. When used on reactor artifact, " + "unpack should be executed after packaging: see MDEP-98.");
        }
        UnArchiver unArchiver;
        try {
            unArchiver = archiverManager.getUnArchiver(type);
            getLog().debug("Found unArchiver by type: " + unArchiver);
        } catch (NoSuchArchiverException e) {
            unArchiver = archiverManager.getUnArchiver(file);
            getLog().debug("Found unArchiver by extension: " + unArchiver);
        }
        if (encoding != null && unArchiver instanceof ZipUnArchiver) {
            ((ZipUnArchiver) unArchiver).setEncoding(encoding);
            getLog().info("Unpacks '" + type + "' with encoding '" + encoding + "'.");
        }
        unArchiver.setUseJvmChmod(useJvmChmod);
        unArchiver.setIgnorePermissions(ignorePermissions);
        unArchiver.setSourceFile(file);
        unArchiver.setDestDirectory(location);
        if (StringUtils.isNotEmpty(excludes) || StringUtils.isNotEmpty(includes)) {
            // Create the selectors that will filter
            // based on include/exclude parameters
            // MDEP-47
            IncludeExcludeFileSelector[] selectors = new IncludeExcludeFileSelector[] { new IncludeExcludeFileSelector() };
            if (StringUtils.isNotEmpty(excludes)) {
                selectors[0].setExcludes(excludes.split(","));
            }
            if (StringUtils.isNotEmpty(includes)) {
                selectors[0].setIncludes(includes.split(","));
            }
            unArchiver.setFileSelectors(selectors);
        }
        if (this.silent) {
            silenceUnarchiver(unArchiver);
        }
        unArchiver.extract();
    } catch (NoSuchArchiverException e) {
        throw new MojoExecutionException("Unknown archiver type", e);
    } catch (ArchiverException e) {
        throw new MojoExecutionException("Error unpacking file: " + file + " to: " + location + "\r\n" + e.toString(), e);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) IncludeExcludeFileSelector(org.codehaus.plexus.components.io.fileselectors.IncludeExcludeFileSelector) NoSuchArchiverException(org.codehaus.plexus.archiver.manager.NoSuchArchiverException) ArchiverException(org.codehaus.plexus.archiver.ArchiverException) ZipUnArchiver(org.codehaus.plexus.archiver.zip.ZipUnArchiver) File(java.io.File) ZipUnArchiver(org.codehaus.plexus.archiver.zip.ZipUnArchiver) UnArchiver(org.codehaus.plexus.archiver.UnArchiver) NoSuchArchiverException(org.codehaus.plexus.archiver.manager.NoSuchArchiverException)

Example 3 with ZipUnArchiver

use of org.codehaus.plexus.archiver.zip.ZipUnArchiver in project xwiki-platform by xwiki.

the class PackageMojo method unzip.

private void unzip(File source, File targetDirectory) throws MojoExecutionException {
    createDirectory(targetDirectory);
    try {
        ZipUnArchiver unArchiver = new ZipUnArchiver();
        unArchiver.enableLogging(new ConsoleLogger(Logger.LEVEL_ERROR, "Package"));
        unArchiver.setSourceFile(source);
        unArchiver.setDestDirectory(targetDirectory);
        unArchiver.setOverwrite(true);
        unArchiver.extract();
    } catch (Exception e) {
        throw new MojoExecutionException(String.format("Error unpacking file [%s] into [%s]", source, targetDirectory), e);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ConsoleLogger(org.codehaus.plexus.logging.console.ConsoleLogger) ZipUnArchiver(org.codehaus.plexus.archiver.zip.ZipUnArchiver) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException)

Example 4 with ZipUnArchiver

use of org.codehaus.plexus.archiver.zip.ZipUnArchiver in project maven-plugins by apache.

the class AbstractDependencyMojo method unpack.

protected void unpack(Artifact artifact, String type, File location, String includes, String excludes, String encoding) throws MojoExecutionException {
    File file = artifact.getFile();
    try {
        logUnpack(file, location, includes, excludes);
        location.mkdirs();
        if (!location.exists()) {
            throw new MojoExecutionException("Location to write unpacked files to could not be created: " + location);
        }
        if (file.isDirectory()) {
            // usual case is a future jar packaging, but there are special cases: classifier and other packaging
            throw new MojoExecutionException("Artifact has not been packaged yet. When used on reactor artifact, " + "unpack should be executed after packaging: see MDEP-98.");
        }
        UnArchiver unArchiver;
        try {
            unArchiver = archiverManager.getUnArchiver(type);
            getLog().debug("Found unArchiver by type: " + unArchiver);
        } catch (NoSuchArchiverException e) {
            unArchiver = archiverManager.getUnArchiver(file);
            getLog().debug("Found unArchiver by extension: " + unArchiver);
        }
        if (encoding != null && unArchiver instanceof ZipUnArchiver) {
            ((ZipUnArchiver) unArchiver).setEncoding(encoding);
            getLog().info("Unpacks '" + type + "' with encoding '" + encoding + "'.");
        }
        unArchiver.setUseJvmChmod(useJvmChmod);
        unArchiver.setIgnorePermissions(ignorePermissions);
        unArchiver.setSourceFile(file);
        unArchiver.setDestDirectory(location);
        if (StringUtils.isNotEmpty(excludes) || StringUtils.isNotEmpty(includes)) {
            // Create the selectors that will filter
            // based on include/exclude parameters
            // MDEP-47
            IncludeExcludeFileSelector[] selectors = new IncludeExcludeFileSelector[] { new IncludeExcludeFileSelector() };
            if (StringUtils.isNotEmpty(excludes)) {
                selectors[0].setExcludes(excludes.split(","));
            }
            if (StringUtils.isNotEmpty(includes)) {
                selectors[0].setIncludes(includes.split(","));
            }
            unArchiver.setFileSelectors(selectors);
        }
        if (this.silent) {
            silenceUnarchiver(unArchiver);
        }
        unArchiver.extract();
    } catch (NoSuchArchiverException e) {
        throw new MojoExecutionException("Unknown archiver type", e);
    } catch (ArchiverException e) {
        throw new MojoExecutionException("Error unpacking file: " + file + " to: " + location + "\r\n" + e.toString(), e);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) IncludeExcludeFileSelector(org.codehaus.plexus.components.io.fileselectors.IncludeExcludeFileSelector) NoSuchArchiverException(org.codehaus.plexus.archiver.manager.NoSuchArchiverException) ArchiverException(org.codehaus.plexus.archiver.ArchiverException) ZipUnArchiver(org.codehaus.plexus.archiver.zip.ZipUnArchiver) File(java.io.File) ZipUnArchiver(org.codehaus.plexus.archiver.zip.ZipUnArchiver) UnArchiver(org.codehaus.plexus.archiver.UnArchiver) NoSuchArchiverException(org.codehaus.plexus.archiver.manager.NoSuchArchiverException)

Aggregations

ZipUnArchiver (org.codehaus.plexus.archiver.zip.ZipUnArchiver)4 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)3 ArchiverException (org.codehaus.plexus.archiver.ArchiverException)3 File (java.io.File)2 UnArchiver (org.codehaus.plexus.archiver.UnArchiver)2 NoSuchArchiverException (org.codehaus.plexus.archiver.manager.NoSuchArchiverException)2 IncludeExcludeFileSelector (org.codehaus.plexus.components.io.fileselectors.IncludeExcludeFileSelector)2 IOException (java.io.IOException)1 MojoFailureException (org.apache.maven.plugin.MojoFailureException)1 ComponentLookupException (org.codehaus.plexus.component.repository.exception.ComponentLookupException)1 ConsoleLogger (org.codehaus.plexus.logging.console.ConsoleLogger)1 FileLockService (org.eclipse.tycho.locking.facade.FileLockService)1 FileLocker (org.eclipse.tycho.locking.facade.FileLocker)1