Search in sources :

Example 11 with ArchiverException

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

the class AddArtifactTask method unpacked.

private void unpacked(Archiver archiver, String destDirectory) throws ArchiveCreationException {
    String outputLocation = destDirectory;
    if ((outputLocation.length() > 0) && !outputLocation.endsWith("/")) {
        outputLocation += "/";
    }
    String[] includesArray = TypeConversionUtils.toStringArray(includes);
    if (includesArray == null) {
        includesArray = DEFAULT_INCLUDES_ARRAY;
    }
    final String[] excludesArray = TypeConversionUtils.toStringArray(excludes);
    try {
        final File artifactFile = artifact.getFile();
        if (artifactFile == null) {
            logger.warn("Skipping artifact: " + artifact.getId() + "; it does not have an associated file or directory.");
        } else if (artifactFile.isDirectory()) {
            logger.debug("Adding artifact directory contents for: " + artifact + " to: " + outputLocation);
            DefaultFileSet fs = DefaultFileSet.fileSet(artifactFile);
            fs.setIncludes(includesArray);
            fs.setExcludes(excludesArray);
            fs.setPrefix(outputLocation);
            fs.setStreamTransformer(transformer);
            archiver.addFileSet(fs);
        } else {
            logger.debug("Unpacking artifact contents for: " + artifact + " to: " + outputLocation);
            logger.debug("includes:\n" + StringUtils.join(includesArray, "\n") + "\n");
            logger.debug("excludes:\n" + (excludesArray == null ? "none" : StringUtils.join(excludesArray, "\n")) + "\n");
            DefaultArchivedFileSet afs = DefaultArchivedFileSet.archivedFileSet(artifactFile);
            afs.setIncludes(includesArray);
            afs.setExcludes(excludesArray);
            afs.setPrefix(outputLocation);
            afs.setStreamTransformer(transformer);
            archiver.addArchivedFileSet(afs, encoding);
        }
    } catch (final ArchiverException e) {
        throw new ArchiveCreationException("Error adding file-set for '" + artifact.getId() + "' to archive: " + e.getMessage(), e);
    }
}
Also used : DefaultFileSet(org.codehaus.plexus.archiver.util.DefaultFileSet) DefaultArchivedFileSet(org.codehaus.plexus.archiver.util.DefaultArchivedFileSet) ArchiverException(org.codehaus.plexus.archiver.ArchiverException) ArchiveCreationException(org.apache.maven.plugins.assembly.archive.ArchiveCreationException) File(java.io.File)

Example 12 with ArchiverException

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

the class EarMojo method copyModules.

private void copyModules(final JavaEEVersion javaEEVersion, List<String> unpackTypesList) throws MojoExecutionException, MojoFailureException {
    try {
        // TODO: With the next major release the modules
        // should be identified by a unique id instead of the
        // the artifactId's only which means this
        // check can be removed.
        // https://issues.apache.org/jira/browse/MEAR-209
        checkModuleUniqueness();
        for (EarModule module : getModules()) {
            final File sourceFile = module.getArtifact().getFile();
            final File destinationFile = buildDestinationFile(getWorkDirectory(), module.getUri());
            if (!sourceFile.isFile()) {
                throw new MojoExecutionException("Cannot copy a directory: " + sourceFile.getAbsolutePath() + "; Did you package/install " + module.getArtifact() + "?");
            }
            if (destinationFile.getCanonicalPath().equals(sourceFile.getCanonicalPath())) {
                getLog().info("Skipping artifact [" + module + "], as it already exists at [" + module.getUri() + "]");
                continue;
            }
            // CHECKSTYLE_OFF: LineLength
            if ((unpackTypesList.contains(module.getType()) && (module.shouldUnpack() == null || module.shouldUnpack())) || (module.shouldUnpack() != null && module.shouldUnpack())) // CHECKSTYLE_ON: LineLength
            {
                getLog().info("Copying artifact [" + module + "] to [" + module.getUri() + "] (unpacked)");
                // Make sure that the destination is a directory to avoid plexus nasty stuff :)
                destinationFile.mkdirs();
                unpack(sourceFile, destinationFile);
                if (skinnyWars && module.changeManifestClasspath()) {
                    changeManifestClasspath(module, destinationFile, javaEEVersion);
                }
            } else {
                if (sourceFile.lastModified() > destinationFile.lastModified()) {
                    getLog().info("Copying artifact [" + module + "] to [" + module.getUri() + "]");
                    FileUtils.copyFile(sourceFile, destinationFile);
                    if (skinnyWars && module.changeManifestClasspath()) {
                        changeManifestClasspath(module, destinationFile, javaEEVersion);
                    }
                } else {
                    getLog().debug("Skipping artifact [" + module + "], as it is already up to date at [" + module.getUri() + "]");
                }
            }
        }
    } catch (IOException e) {
        throw new MojoExecutionException("Error copying EAR modules", e);
    } catch (ArchiverException e) {
        throw new MojoExecutionException("Error unpacking EAR modules", e);
    } catch (NoSuchArchiverException e) {
        throw new MojoExecutionException("No Archiver found for EAR modules", e);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) NoSuchArchiverException(org.codehaus.plexus.archiver.manager.NoSuchArchiverException) ArchiverException(org.codehaus.plexus.archiver.ArchiverException) IOException(java.io.IOException) File(java.io.File) NoSuchArchiverException(org.codehaus.plexus.archiver.manager.NoSuchArchiverException)

Example 13 with ArchiverException

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

the class EarMojo method changeManifestClasspath.

private void changeManifestClasspath(EarModule module, File original, JavaEEVersion javaEEVersion) throws MojoFailureException {
    try {
        File workDirectory;
        // Handle the case that the destination might be a directory (project-038)
        if (original.isFile()) {
            // Create a temporary work directory
            // MEAR-167 use uri as directory to prevent merging of artifacts with the same artifactId
            workDirectory = new File(new File(getTempFolder(), "temp"), module.getUri());
            workDirectory.mkdirs();
            getLog().debug("Created a temporary work directory: " + workDirectory.getAbsolutePath());
            // Unpack the archive to a temporary work directory
            zipUnArchiver.setSourceFile(original);
            zipUnArchiver.setDestDirectory(workDirectory);
            zipUnArchiver.extract();
        } else {
            workDirectory = original;
        }
        // Create a META-INF/MANIFEST.MF file if it doesn't exist (project-038)
        File metaInfDirectory = new File(workDirectory, "META-INF");
        boolean newMetaInfCreated = metaInfDirectory.mkdirs();
        if (newMetaInfCreated) {
            // CHECKSTYLE_OFF: LineLength
            getLog().debug("This project did not have a META-INF directory before, so a new directory was created.");
        // CHECKSTYLE_ON: LineLength
        }
        File newCreatedManifestFile = new File(metaInfDirectory, "MANIFEST.MF");
        boolean newManifestCreated = newCreatedManifestFile.createNewFile();
        if (newManifestCreated) {
            // CHECKSTYLE_OFF: LineLength
            getLog().debug("This project did not have a META-INF/MANIFEST.MF file before, so a new file was created.");
        // CHECKSTYLE_ON: LineLength
        }
        // Read the manifest from disk
        Manifest mf = new Manifest(new FileInputStream(newCreatedManifestFile));
        Attribute classPath = mf.getMainSection().getAttribute("Class-Path");
        List<String> classPathElements = new ArrayList<String>();
        if (classPath != null) {
            classPathElements.addAll(Arrays.asList(classPath.getValue().split(" ")));
        } else {
            classPath = new Attribute("Class-Path", "");
        }
        // Remove JAR modules
        for (JarModule jm : getAllJarModules()) {
            if (module.getLibDir() != null) {
                // MEAR-189:
                // We use the original name, cause in case of fileNameMapping to no-version/full
                // we could not not delete it and it will end up in the resulting EAR and the WAR
                // will not be cleaned up.
                File artifact = new File(new File(workDirectory, module.getLibDir()), jm.getOriginalBundleFileName());
                // the artifact is not found. Therefore respect the current fileNameMapping additionally.
                if (!artifact.exists()) {
                    artifact = new File(new File(workDirectory, module.getLibDir()), jm.getBundleFileName());
                }
                if (artifact.exists()) {
                    getLog().debug(" -> Artifact to delete: " + artifact);
                    if (!artifact.delete()) {
                        getLog().error("Could not delete '" + artifact + "'");
                    }
                }
            }
        }
        // Modify the classpath entries in the manifest
        for (EarModule o : getModules()) {
            if (o instanceof JarModule) {
                JarModule jm = (JarModule) o;
                if (classPathElements.contains(jm.getBundleFileName())) {
                    classPathElements.set(classPathElements.indexOf(jm.getBundleFileName()), jm.getUri());
                } else {
                    if (!skipClassPathModification) {
                        classPathElements.add(jm.getUri());
                    } else {
                        if (javaEEVersion.lt(JavaEEVersion.FIVE) || defaultLibBundleDir == null) {
                            classPathElements.add(jm.getUri());
                        }
                    }
                }
            }
        }
        classPath.setValue(StringUtils.join(classPathElements.iterator(), " "));
        mf.getMainSection().addConfiguredAttribute(classPath);
        // Write the manifest to disk
        PrintWriter pw = new PrintWriter(newCreatedManifestFile);
        mf.write(pw);
        pw.close();
        if (original.isFile()) {
            // Pack up the archive again from the work directory
            if (!original.delete()) {
                getLog().error("Could not delete original artifact file " + original);
            }
            getLog().debug("Zipping module");
            zipArchiver.setDestFile(original);
            zipArchiver.addDirectory(workDirectory);
            zipArchiver.createArchive();
        }
    } catch (ManifestException e) {
        throw new MojoFailureException(e.getMessage());
    } catch (ZipException e) {
        throw new MojoFailureException(e.getMessage());
    } catch (IOException e) {
        throw new MojoFailureException(e.getMessage());
    } catch (ArchiverException e) {
        throw new MojoFailureException(e.getMessage());
    }
}
Also used : Attribute(org.codehaus.plexus.archiver.jar.Manifest.Attribute) NoSuchArchiverException(org.codehaus.plexus.archiver.manager.NoSuchArchiverException) ArchiverException(org.codehaus.plexus.archiver.ArchiverException) ArrayList(java.util.ArrayList) MojoFailureException(org.apache.maven.plugin.MojoFailureException) ZipException(java.util.zip.ZipException) IOException(java.io.IOException) Manifest(org.codehaus.plexus.archiver.jar.Manifest) ManifestException(org.codehaus.plexus.archiver.jar.ManifestException) FileInputStream(java.io.FileInputStream) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 14 with ArchiverException

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

the class AddDirectoryTaskTest method testAddDirectory_ShouldAddDirectory.

public void testAddDirectory_ShouldAddDirectory() throws ArchiveCreationException {
    File dir = fileManager.createTempDir();
    try {
        archiver.addFileSet((FileSet) anyObject());
    } catch (ArchiverException e) {
        fail("Should never happen.");
    }
    configureModeExpectations(-1, -1, -1, -1, false);
    mockManager.replayAll();
    AddDirectoryTask task = new AddDirectoryTask(dir);
    task.setOutputDirectory("dir");
    task.execute(archiver);
    mockManager.verifyAll();
}
Also used : ArchiverException(org.codehaus.plexus.archiver.ArchiverException) File(java.io.File)

Example 15 with ArchiverException

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

the class AbstractLineAggregatingHandler method addToArchive.

void addToArchive(final Archiver archiver) {
    for (final Map.Entry<String, List<String>> entry : catalog.entrySet()) {
        final String name = entry.getKey();
        final String fname = new File(name).getName();
        PrintWriter writer = null;
        File f;
        try {
            f = File.createTempFile("assembly-" + fname, ".tmp");
            f.deleteOnExit();
            writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(f), getEncoding()));
            for (final String line : entry.getValue()) {
                writer.println(line);
            }
            writer.close();
            writer = null;
        } catch (final IOException e) {
            throw new ArchiverException("Error adding aggregated content for: " + fname + " to finalize archive creation. Reason: " + e.getMessage(), e);
        } finally {
            IOUtil.close(writer);
        }
        excludeOverride = true;
        archiver.addFile(f, name);
        excludeOverride = false;
    }
}
Also used : ArchiverException(org.codehaus.plexus.archiver.ArchiverException) FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) List(java.util.List) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File) PrintWriter(java.io.PrintWriter)

Aggregations

ArchiverException (org.codehaus.plexus.archiver.ArchiverException)51 File (java.io.File)33 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)26 IOException (java.io.IOException)25 NoSuchArchiverException (org.codehaus.plexus.archiver.manager.NoSuchArchiverException)17 UnArchiver (org.codehaus.plexus.archiver.UnArchiver)9 Artifact (org.apache.maven.artifact.Artifact)8 ManifestException (org.codehaus.plexus.archiver.jar.ManifestException)8 DependencyResolutionRequiredException (org.apache.maven.artifact.DependencyResolutionRequiredException)7 MavenArchiver (org.apache.maven.archiver.MavenArchiver)6 ArchiveCreationException (org.apache.maven.plugins.assembly.archive.ArchiveCreationException)5 ArrayList (java.util.ArrayList)3 MojoFailureException (org.apache.maven.plugin.MojoFailureException)3 Archiver (org.codehaus.plexus.archiver.Archiver)3 JarArchiver (org.codehaus.plexus.archiver.jar.JarArchiver)3 FileInputStream (java.io.FileInputStream)2 FileOutputStream (java.io.FileOutputStream)2 FileWriter (java.io.FileWriter)2 OutputStreamWriter (java.io.OutputStreamWriter)2 PrintWriter (java.io.PrintWriter)2