Search in sources :

Example 6 with UnArchiver

use of org.codehaus.plexus.archiver.UnArchiver in project midpoint by Evolveum.

the class SchemaDistMojo method unpack.

private void unpack(ArtifactItem artifactItem, File destDir) throws MojoExecutionException {
    Artifact artifact = artifactItem.getArtifact();
    File file = artifact.getFile();
    if (file == null) {
        throw new MojoExecutionException("No file for artifact " + artifact);
    }
    if (file.isDirectory()) {
        try {
            FileUtils.copyDirectory(file, destDir);
        } catch (IOException e) {
            throw new MojoExecutionException("Error copying directory " + file + " to " + destDir + ": " + e.getMessage(), e);
        }
    } else {
        try {
            UnArchiver unArchiver = archiverManager.getUnArchiver(artifact.getType());
            unArchiver.setSourceFile(file);
            unArchiver.setDestDirectory(destDir);
            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);
            }
            unArchiver.extract();
        } catch (ArchiverException | NoSuchArchiverException e) {
            throw new MojoExecutionException("Error unpacking file: " + file + " to: " + destDir + "\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) Artifact(org.apache.maven.artifact.Artifact) UnArchiver(org.codehaus.plexus.archiver.UnArchiver) NoSuchArchiverException(org.codehaus.plexus.archiver.manager.NoSuchArchiverException)

Example 7 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)

Example 8 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 9 with UnArchiver

use of org.codehaus.plexus.archiver.UnArchiver in project sling by apache.

the class PreparePackageMojoTest method getMojoUnderTest.

private PreparePackageMojo getMojoUnderTest(String... knownArtifacts) throws Exception {
    File mrr = getMavenRepoRoot();
    ArtifactHandler ah = Mockito.mock(ArtifactHandler.class);
    ArtifactHandlerManager ahm = Mockito.mock(ArtifactHandlerManager.class);
    Mockito.when(ahm.getArtifactHandler(Mockito.anyString())).thenReturn(ah);
    Set<org.apache.maven.artifact.Artifact> artifacts = new HashSet<>();
    for (String s : knownArtifacts) {
        String[] parts = s.split("[/]");
        switch(parts.length) {
            case 3:
                artifacts.add(getMavenArtifact(mrr, ah, parts[0], parts[1], parts[2]));
                break;
            case 4:
                artifacts.add(getMavenArtifact(mrr, ah, parts[0], parts[1], parts[2], parts[3]));
                break;
            default:
                throw new IllegalStateException(s);
        }
    }
    MavenProject mavenPrj = new MavenProject();
    Build build = new Build();
    Path tempDir = Files.createTempDirectory(getClass().getSimpleName());
    build.setOutputDirectory(tempDir.toString());
    build.setDirectory(tempDir.toString());
    mavenPrj.setBuild(build);
    mavenPrj.setDependencyArtifacts(artifacts);
    PreparePackageMojo ppm = new PreparePackageMojo();
    ppm.mavenSession = Mockito.mock(MavenSession.class);
    ppm.project = mavenPrj;
    ArchiverManager am = Mockito.mock(ArchiverManager.class);
    UnArchiver ua = Mockito.mock(UnArchiver.class);
    Mockito.when(am.getUnArchiver(Mockito.isA(File.class))).thenReturn(ua);
    setPrivateField(ppm, "archiverManager", am);
    setPrivateField(ppm, "artifactHandlerManager", ahm);
    setPrivateField(ppm, "resolver", Mockito.mock(ArtifactResolver.class));
    return ppm;
}
Also used : Path(java.nio.file.Path) ArchiverManager(org.codehaus.plexus.archiver.manager.ArchiverManager) DefaultArtifact(org.apache.maven.artifact.DefaultArtifact) ArtifactResolver(org.apache.maven.artifact.resolver.ArtifactResolver) MavenSession(org.apache.maven.execution.MavenSession) ArtifactHandler(org.apache.maven.artifact.handler.ArtifactHandler) MavenProject(org.apache.maven.project.MavenProject) Build(org.apache.maven.model.Build) ArtifactHandlerManager(org.apache.maven.artifact.handler.manager.ArtifactHandlerManager) JarFile(java.util.jar.JarFile) File(java.io.File) UnArchiver(org.codehaus.plexus.archiver.UnArchiver) HashSet(java.util.HashSet)

Example 10 with UnArchiver

use of org.codehaus.plexus.archiver.UnArchiver in project sling by apache.

the class PreparePackageMojo method unpack.

private void unpack(File source, File destination) throws MojoExecutionException {
    getLog().info("Unpacking " + source.getPath() + " to\n  " + destination.getPath());
    try {
        destination.mkdirs();
        UnArchiver unArchiver = archiverManager.getUnArchiver(source);
        unArchiver.setSourceFile(source);
        unArchiver.setDestDirectory(destination);
        unArchiver.extract();
    } catch (NoSuchArchiverException e) {
        throw new MojoExecutionException("Unable to find archiver for " + source.getPath(), e);
    } catch (ArchiverException e) {
        throw new MojoExecutionException("Unable to unpack " + source.getPath(), e);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) NoSuchArchiverException(org.codehaus.plexus.archiver.manager.NoSuchArchiverException) ArchiverException(org.codehaus.plexus.archiver.ArchiverException) UnArchiver(org.codehaus.plexus.archiver.UnArchiver) NoSuchArchiverException(org.codehaus.plexus.archiver.manager.NoSuchArchiverException)

Aggregations

UnArchiver (org.codehaus.plexus.archiver.UnArchiver)11 ArchiverException (org.codehaus.plexus.archiver.ArchiverException)9 NoSuchArchiverException (org.codehaus.plexus.archiver.manager.NoSuchArchiverException)9 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)5 File (java.io.File)4 Artifact (org.apache.maven.artifact.Artifact)3 IncludeExcludeFileSelector (org.codehaus.plexus.components.io.fileselectors.IncludeExcludeFileSelector)3 DefaultArtifact (org.apache.maven.artifact.DefaultArtifact)2 ArchiverManager (org.codehaus.plexus.archiver.manager.ArchiverManager)2 ZipUnArchiver (org.codehaus.plexus.archiver.zip.ZipUnArchiver)2 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 JarFile (java.util.jar.JarFile)1 ArtifactHandler (org.apache.maven.artifact.handler.ArtifactHandler)1 ArtifactHandlerManager (org.apache.maven.artifact.handler.manager.ArtifactHandlerManager)1 ArtifactRepository (org.apache.maven.artifact.repository.ArtifactRepository)1 ArtifactResolutionException (org.apache.maven.artifact.resolver.ArtifactResolutionException)1