Search in sources :

Example 16 with UnArchiver

use of org.codehaus.plexus.archiver.UnArchiver 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 17 with UnArchiver

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

the class EmbedderMojoBehaviour method shouldUnpackViewResources.

@Test
public void shouldUnpackViewResources() throws MojoExecutionException, MojoFailureException, NoSuchArchiverException, ArchiverException {
    // Given
    UnpackViewResources mojo = new UnpackViewResources() {

        @Override
        protected Embedder newEmbedder() {
            return new Embedder();
        }
    };
    ArchiverManager archiveManager = mock(ArchiverManager.class);
    MavenProject project = mock(MavenProject.class);
    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);
    UnArchiver coreArchiver = mock(UnArchiver.class);
    UnArchiver siteArchiver = mock(UnArchiver.class);
    // When
    mojo.project = project;
    mojo.archiverManager = archiveManager;
    mojo.resourceIncludes = "ftl/*";
    mojo.resourcesExcludes = "com/*";
    when(project.getArtifacts()).thenReturn(allArtifacts);
    when(project.getBuild()).thenReturn(build);
    when(archiveManager.getUnArchiver(coreFile)).thenReturn(coreArchiver);
    when(archiveManager.getUnArchiver(siteFile)).thenReturn(siteArchiver);
    // default view directory
    unpackTo(mojo, null);
    unpackTo(mojo, new File(System.getProperty("java.io.tmpdir") + "/jbehave/view"));
    // Then
    verify(coreArchiver, times(2)).extract();
    verify(siteArchiver, times(2)).extract();
}
Also used : ArchiverManager(org.codehaus.plexus.archiver.manager.ArchiverManager) MavenProject(org.apache.maven.project.MavenProject) Build(org.apache.maven.model.Build) Embedder(org.jbehave.core.embedder.Embedder) InjectableEmbedder(org.jbehave.core.InjectableEmbedder) Matchers.containsString(org.hamcrest.Matchers.containsString) File(java.io.File) Artifact(org.apache.maven.artifact.Artifact) UnArchiver(org.codehaus.plexus.archiver.UnArchiver) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 18 with UnArchiver

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

the class EmbedderMojoBehaviour method shouldNotIgnoreFailureInUnpackingViewResources.

@Test(expected = MojoExecutionException.class)
public void shouldNotIgnoreFailureInUnpackingViewResources() throws MojoExecutionException, MojoFailureException, NoSuchArchiverException, ArchiverException {
    // Given
    UnpackViewResources mojo = new UnpackViewResources() {

        @Override
        protected Embedder newEmbedder() {
            return new Embedder();
        }
    };
    ArchiverManager archiveManager = mock(ArchiverManager.class);
    MavenProject project = mock(MavenProject.class);
    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);
    UnArchiver coreArchiver = mock(UnArchiver.class);
    UnArchiver siteArchiver = mock(UnArchiver.class);
    // When
    mojo.project = project;
    mojo.archiverManager = archiveManager;
    when(project.getArtifacts()).thenReturn(allArtifacts);
    when(project.getBuild()).thenReturn(build);
    when(archiveManager.getUnArchiver(coreFile)).thenReturn(coreArchiver);
    when(archiveManager.getUnArchiver(siteFile)).thenReturn(siteArchiver);
    Mockito.doThrow(new ArchiverException("bum")).when(siteArchiver).extract();
    mojo.execute();
    // Then
    verify(coreArchiver).extract();
// and fail as expected ...
}
Also used : ArchiverManager(org.codehaus.plexus.archiver.manager.ArchiverManager) MavenProject(org.apache.maven.project.MavenProject) NoSuchArchiverException(org.codehaus.plexus.archiver.manager.NoSuchArchiverException) ArchiverException(org.codehaus.plexus.archiver.ArchiverException) Build(org.apache.maven.model.Build) Embedder(org.jbehave.core.embedder.Embedder) InjectableEmbedder(org.jbehave.core.InjectableEmbedder) Matchers.containsString(org.hamcrest.Matchers.containsString) File(java.io.File) Artifact(org.apache.maven.artifact.Artifact) UnArchiver(org.codehaus.plexus.archiver.UnArchiver) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 19 with UnArchiver

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

the class AssemblyFileUtilsTest method testUnpack_ShouldSetSourceAndDestinationAndCallExtract.

public void testUnpack_ShouldSetSourceAndDestinationAndCallExtract() throws IOException, ArchiveExpansionException, NoSuchArchiverException {
    EasyMockSupport mockManager = new EasyMockSupport();
    File source = fileManager.createTempFile();
    File destDir = fileManager.createTempDir();
    UnArchiver unarchiver = mockManager.createMock(UnArchiver.class);
    ArchiverManager archiverManager = mockManager.createMock(ArchiverManager.class);
    try {
        expect(archiverManager.getUnArchiver(source)).andReturn(unarchiver);
    } catch (NoSuchArchiverException e) {
        fail("Should never happen.");
    }
    unarchiver.setSourceFile(source);
    unarchiver.setDestDirectory(destDir);
    try {
        unarchiver.extract();
    } catch (ArchiverException e) {
        fail("Should never happen.");
    }
    mockManager.replayAll();
    AssemblyFileUtils.unpack(source, destDir, archiverManager);
    mockManager.verifyAll();
}
Also used : EasyMockSupport(org.easymock.classextension.EasyMockSupport) ArchiverManager(org.codehaus.plexus.archiver.manager.ArchiverManager) ArchiverException(org.codehaus.plexus.archiver.ArchiverException) NoSuchArchiverException(org.codehaus.plexus.archiver.manager.NoSuchArchiverException) File(java.io.File) UnArchiver(org.codehaus.plexus.archiver.UnArchiver) NoSuchArchiverException(org.codehaus.plexus.archiver.manager.NoSuchArchiverException)

Example 20 with UnArchiver

use of org.codehaus.plexus.archiver.UnArchiver 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

UnArchiver (org.codehaus.plexus.archiver.UnArchiver)20 ArchiverException (org.codehaus.plexus.archiver.ArchiverException)14 NoSuchArchiverException (org.codehaus.plexus.archiver.manager.NoSuchArchiverException)14 File (java.io.File)10 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)9 Artifact (org.apache.maven.artifact.Artifact)7 IncludeExcludeFileSelector (org.codehaus.plexus.components.io.fileselectors.IncludeExcludeFileSelector)6 ArchiverManager (org.codehaus.plexus.archiver.manager.ArchiverManager)4 HashSet (java.util.HashSet)3 DefaultArtifact (org.apache.maven.artifact.DefaultArtifact)3 Build (org.apache.maven.model.Build)3 MavenProject (org.apache.maven.project.MavenProject)3 ArtifactResolverException (org.apache.maven.shared.artifact.resolve.ArtifactResolverException)3 ZipUnArchiver (org.codehaus.plexus.archiver.zip.ZipUnArchiver)3 ArrayList (java.util.ArrayList)2 LinkedHashSet (java.util.LinkedHashSet)2 ArtifactResolutionException (org.apache.maven.artifact.resolver.ArtifactResolutionException)2 ArtifactFilter (org.apache.maven.artifact.resolver.filter.ArtifactFilter)2 MavenReportException (org.apache.maven.reporting.MavenReportException)2 ArtifactIncludeFilterTransformer (org.apache.maven.shared.artifact.filter.resolve.transform.ArtifactIncludeFilterTransformer)2