Search in sources :

Example 86 with ArchiverException

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

the class SignRepositoryArtifactsMojo method execute.

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    var artifactsXml = new File(repository, "artifacts.xml");
    var artifactsXmlXz = new File(repository, "artifacts.xml.xz");
    var artifactsJar = new File(repository, "artifacts.jar");
    if (!artifactsXml.exists()) {
        if (artifactsXmlXz.exists()) {
            xzUnarchiver.setSourceFile(artifactsXmlXz);
            xzUnarchiver.setDestFile(artifactsXml);
            xzUnarchiver.extract();
            artifactsXmlXz.delete();
        }
        if (artifactsJar.exists()) {
            zipUnArchiver.setSourceFile(artifactsJar);
            zipUnArchiver.setDestDirectory(repository);
            zipUnArchiver.extract();
            artifactsJar.delete();
        }
    }
    Xpp3Dom dom = null;
    try (var stream = new FileInputStream(artifactsXml)) {
        dom = Xpp3DomBuilder.build(stream, StandardCharsets.UTF_8.displayName());
    } catch (IOException | XmlPullParserException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    }
    ProxySignerWithPublicKeyAccess signer = newSigner(project);
    String armoredPublicKey = signer.getPublicKeys();
    for (var artifact : dom.getChild("artifacts").getChildren("artifact")) {
        Xpp3Dom properties = artifact.getChild("properties");
        if (Arrays.stream(properties.getChildren()).anyMatch(property -> IArtifactDescriptor.FORMAT.equals(property.getAttribute("name")) && IArtifactDescriptor.FORMAT_PACKED.equals(property.getAttribute("value")))) {
            // skip packed artifacts
            continue;
        }
        var file = new File(repository, "plugins/" + artifact.getAttribute("id") + '_' + artifact.getAttribute("version") + ".jar");
        if (!file.canRead()) {
            continue;
        }
        if (skipIfJarsigned && isJarSigned(file)) {
            continue;
        }
        var signatureFile = signer.generateSignatureForArtifact(file);
        String signature;
        try {
            signature = Files.readString(signatureFile.toPath());
            var signatureProperty = new Xpp3Dom("property");
            signatureProperty.setAttribute("name", "pgp.signatures");
            signatureProperty.setAttribute("value", signature);
            properties.addChild(signatureProperty);
            properties.setAttribute("size", Integer.toString(Integer.parseInt(properties.getAttribute("size")) + 1));
            if (addPublicKeysToArtifacts) {
                var publicKeyProperty = new Xpp3Dom("property");
                publicKeyProperty.setAttribute("name", "pgp.publicKeys");
                publicKeyProperty.setAttribute("value", armoredPublicKey);
                properties.addChild(publicKeyProperty);
                properties.setAttribute("size", Integer.toString(Integer.parseInt(properties.getAttribute("size")) + 1));
            }
        } catch (IOException e) {
            throw new MojoExecutionException(e.getMessage(), e);
        }
    }
    if (addPublicKeyToRepo) {
        Xpp3Dom repositoryProperties = dom.getChild("properties");
        repositoryProperties.setAttribute("size", Integer.toString(Integer.parseInt(repositoryProperties.getAttribute("size")) + 1));
        var signersProperty = new Xpp3Dom("property");
        signersProperty.setAttribute("name", "pgp.publicKeys");
        signersProperty.setAttribute("value", armoredPublicKey);
        repositoryProperties.addChild(signersProperty);
    }
    try (var writer = new FileWriter(artifactsXml)) {
        Xpp3DomWriter.write(writer, dom);
    } catch (IOException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    }
    xzArchiver.setDestFile(artifactsXmlXz);
    xzArchiver.addFile(artifactsXml, artifactsXml.getName());
    try {
        xzArchiver.createArchive();
    } catch (ArchiverException | IOException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    }
    zipArchiver.setDestFile(artifactsJar);
    zipArchiver.addFile(artifactsXml, artifactsXml.getName());
    try {
        zipArchiver.createArchive();
    } catch (ArchiverException | IOException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    }
}
Also used : Xpp3Dom(org.codehaus.plexus.util.xml.Xpp3Dom) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ArchiverException(org.codehaus.plexus.archiver.ArchiverException) ProxySignerWithPublicKeyAccess(org.apache.maven.plugins.gpg.ProxySignerWithPublicKeyAccess) FileWriter(java.io.FileWriter) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 87 with ArchiverException

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

the class DefaultEquinoxInstallationFactory method unpack.

protected void unpack(File source, File destination) {
    UnArchiver unzip;
    try {
        unzip = plexus.lookup(UnArchiver.class, "zip");
    } catch (ComponentLookupException e) {
        throw new RuntimeException("Could not lookup required component", e);
    }
    unzip.setIgnorePermissions(true);
    destination.mkdirs();
    unzip.setSourceFile(source);
    unzip.setDestDirectory(destination);
    try {
        unzip.extract();
    } catch (ArchiverException e) {
        throw new RuntimeException("Unable to unpack jar " + source, e);
    }
}
Also used : ArchiverException(org.codehaus.plexus.archiver.ArchiverException) ComponentLookupException(org.codehaus.plexus.component.repository.exception.ComponentLookupException) UnArchiver(org.codehaus.plexus.archiver.UnArchiver)

Example 88 with ArchiverException

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

the class TychoOsgiRuntimeLocator method addRuntimeArtifact.

private void addRuntimeArtifact(EquinoxRuntimeDescription description, MavenSession session, Dependency dependency) throws MavenExecutionException {
    Artifact artifact = resolveDependency(session, dependency);
    if ("zip".equals(dependency.getType())) {
        File artifactFile = new File(session.getLocalRepository().getBasedir(), session.getLocalRepository().pathOf(artifact));
        File eclipseDir = new File(artifactFile.getParentFile(), "eclipse");
        FileLocker locker = fileLockService.getFileLocker(artifactFile);
        locker.lock();
        try {
            if (!eclipseDir.exists() || artifact.isSnapshot()) {
                logger.debug("Extracting Tycho's OSGi runtime");
                if (artifact.getFile().lastModified() > eclipseDir.lastModified()) {
                    logger.debug("Unpacking Tycho's OSGi runtime to " + eclipseDir);
                    try {
                        FileUtils.deleteDirectory(eclipseDir);
                    } catch (IOException e) {
                        logger.warn("Failed to delete Tycho's OSGi runtime " + eclipseDir + ": " + e.getMessage());
                    }
                    eclipseDir.mkdirs();
                    unArchiver.setSourceFile(artifact.getFile());
                    unArchiver.setDestDirectory(eclipseDir);
                    try {
                        unArchiver.extract();
                    } catch (ArchiverException e) {
                        throw new MavenExecutionException("Failed to unpack Tycho's OSGi runtime: " + e.getMessage(), e);
                    }
                    eclipseDir.setLastModified(artifact.getFile().lastModified());
                }
            }
        } finally {
            locker.release();
        }
        description.addInstallation(eclipseDir);
    } else {
        description.addBundle(artifact.getFile());
    }
}
Also used : FileLocker(org.eclipse.tycho.locking.facade.FileLocker) MavenExecutionException(org.apache.maven.MavenExecutionException) ArchiverException(org.codehaus.plexus.archiver.ArchiverException) IOException(java.io.IOException) File(java.io.File) Artifact(org.apache.maven.artifact.Artifact)

Example 89 with ArchiverException

use of org.codehaus.plexus.archiver.ArchiverException in project jbehave-core by jbehave.

the class EmbedderMojoBehaviour method shouldNotIgnoreFailureInUnpackingViewResources.

@Test
void shouldNotIgnoreFailureInUnpackingViewResources() throws NoSuchArchiverException, ArchiverException {
    // Given
    File coreFile = new File("core");
    Artifact coreResources = mock(Artifact.class);
    when(coreResources.getArtifactId()).thenReturn("jbehave-core");
    when(coreResources.getType()).thenReturn("zip");
    when(coreResources.getFile()).thenReturn(coreFile);
    File siteFile = new File("site");
    Artifact siteResources = mock(Artifact.class);
    when(siteResources.getArtifactId()).thenReturn("jbehave-site-resources");
    when(siteResources.getType()).thenReturn("zip");
    when(siteResources.getFile()).thenReturn(siteFile);
    Set<Artifact> allArtifacts = new HashSet<>();
    allArtifacts.add(coreResources);
    allArtifacts.add(siteResources);
    String buildDirectory = "target";
    Build build = new Build();
    build.setDirectory(buildDirectory);
    MavenProject project = mock(MavenProject.class);
    when(project.getArtifacts()).thenReturn(allArtifacts);
    when(project.getBuild()).thenReturn(build);
    UnArchiver coreArchiver = mock(UnArchiver.class);
    UnArchiver siteArchiver = mock(UnArchiver.class);
    ArchiverManager archiveManager = mock(ArchiverManager.class);
    when(archiveManager.getUnArchiver(coreFile)).thenReturn(coreArchiver);
    when(archiveManager.getUnArchiver(siteFile)).thenReturn(siteArchiver);
    UnpackViewResources mojo = new UnpackViewResources() {

        @Override
        protected Embedder newEmbedder() {
            return new Embedder();
        }
    };
    mojo.project = project;
    mojo.archiverManager = archiveManager;
    // When
    doThrow(new ArchiverException("bum")).when(siteArchiver).extract();
    // Then fail as expected ...
    assertThrows(MojoExecutionException.class, mojo::execute);
}
Also used : ArchiverManager(org.codehaus.plexus.archiver.manager.ArchiverManager) NoSuchArchiverException(org.codehaus.plexus.archiver.manager.NoSuchArchiverException) ArchiverException(org.codehaus.plexus.archiver.ArchiverException) Embedder(org.jbehave.core.embedder.Embedder) InjectableEmbedder(org.jbehave.core.InjectableEmbedder) Matchers.containsString(org.hamcrest.Matchers.containsString) Artifact(org.apache.maven.artifact.Artifact) MavenProject(org.apache.maven.project.MavenProject) Build(org.apache.maven.model.Build) File(java.io.File) UnArchiver(org.codehaus.plexus.archiver.UnArchiver) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 90 with ArchiverException

use of org.codehaus.plexus.archiver.ArchiverException in project alfresco-maven by Acosix.

the class AmpUnArchiver method loadMetaFile.

protected <T> T loadMetaFile(final File context, final String relativePath, final FileReader<T> reader) throws ArchiverException {
    this.getLogger().debug("Attempting to resolve meta file " + relativePath + " in context " + context);
    T meta = null;
    try {
        if (context.isFile()) {
            try (ZipFile archiveCandidate = new ZipFile(context, this.encoding, true)) {
                final ZipArchiveEntry zae = archiveCandidate.getEntry(relativePath);
                if (zae != null && !zae.isDirectory()) {
                    try (InputStream is = archiveCandidate.getInputStream(zae)) {
                        meta = reader.readFile(is);
                        this.getLogger().debug("Succesfully read meta file " + relativePath + " from context " + context);
                    }
                }
            }
        } else {
            final File propertiesFile = Paths.get(context.getPath(), relativePath).toFile();
            if (propertiesFile.exists() && propertiesFile.isFile()) {
                try (InputStream is = new FileInputStream(propertiesFile)) {
                    meta = reader.readFile(is);
                    this.getLogger().debug("Succesfully read meta file" + relativePath + " from context " + context);
                }
            }
        }
    } catch (final IOException ioex) {
        throw new ArchiverException("Error loading file " + relativePath + " from " + context.getAbsolutePath(), ioex);
    }
    return meta;
}
Also used : ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) ArchiverException(org.codehaus.plexus.archiver.ArchiverException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) IOException(java.io.IOException) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

ArchiverException (org.codehaus.plexus.archiver.ArchiverException)99 File (java.io.File)69 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)54 IOException (java.io.IOException)48 NoSuchArchiverException (org.codehaus.plexus.archiver.manager.NoSuchArchiverException)28 UnArchiver (org.codehaus.plexus.archiver.UnArchiver)18 ManifestException (org.codehaus.plexus.archiver.jar.ManifestException)15 MavenArchiver (org.apache.maven.archiver.MavenArchiver)14 Artifact (org.apache.maven.artifact.Artifact)14 DependencyResolutionRequiredException (org.apache.maven.artifact.DependencyResolutionRequiredException)14 Archiver (org.codehaus.plexus.archiver.Archiver)8 Resource (org.apache.maven.model.Resource)7 IncludeExcludeFileSelector (org.codehaus.plexus.components.io.fileselectors.IncludeExcludeFileSelector)7 MojoFailureException (org.apache.maven.plugin.MojoFailureException)6 DefaultFileSet (org.codehaus.plexus.archiver.util.DefaultFileSet)6 ArrayList (java.util.ArrayList)5 ArchiveCreationException (org.apache.maven.plugins.assembly.archive.ArchiveCreationException)5 MavenProject (org.apache.maven.project.MavenProject)5 FileInputStream (java.io.FileInputStream)4 FileWriter (java.io.FileWriter)4