Search in sources :

Example 21 with ArchiverException

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

the class AbstractSourceJarMojo method packageSources.

/**
     * @param theProjects {@link MavenProject}
     * @throws MojoExecutionException in case of an error.
     */
protected void packageSources(List<MavenProject> theProjects) throws MojoExecutionException {
    if (project.getArtifact().getClassifier() != null) {
        getLog().warn("NOT adding sources to artifacts with classifier as Maven only supports one classifier " + "per artifact. Current artifact [" + project.getArtifact().getId() + "] has a [" + project.getArtifact().getClassifier() + "] classifier.");
        return;
    }
    MavenArchiver archiver = createArchiver();
    for (MavenProject pItem : theProjects) {
        MavenProject subProject = getProject(pItem);
        if ("pom".equals(subProject.getPackaging())) {
            continue;
        }
        archiveProjectContent(subProject, archiver.getArchiver());
    }
    if (archiver.getArchiver().getResources().hasNext() || forceCreation) {
        if (useDefaultManifestFile && defaultManifestFile.exists() && archive.getManifestFile() == null) {
            getLog().info("Adding existing MANIFEST to archive. Found under: " + defaultManifestFile.getPath());
            archive.setManifestFile(defaultManifestFile);
        }
        File outputFile = new File(outputDirectory, finalName + "-" + getClassifier() + getExtension());
        try {
            archiver.setOutputFile(outputFile);
            archive.setForced(forceCreation);
            archiver.createArchive(session, project, archive);
        } catch (IOException e) {
            throw new MojoExecutionException("Error creating source archive: " + e.getMessage(), e);
        } catch (ArchiverException e) {
            throw new MojoExecutionException("Error creating source archive: " + e.getMessage(), e);
        } catch (DependencyResolutionRequiredException e) {
            throw new MojoExecutionException("Error creating source archive: " + e.getMessage(), e);
        } catch (ManifestException e) {
            throw new MojoExecutionException("Error creating source archive: " + e.getMessage(), e);
        }
        if (attach) {
            projectHelper.attachArtifact(project, getType(), getClassifier(), outputFile);
        } else {
            getLog().info("NOT adding java-sources to attached artifacts list.");
        }
    } else {
        getLog().info("No sources in project. Archive not created.");
    }
}
Also used : DependencyResolutionRequiredException(org.apache.maven.artifact.DependencyResolutionRequiredException) MavenProject(org.apache.maven.project.MavenProject) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ArchiverException(org.codehaus.plexus.archiver.ArchiverException) MavenArchiver(org.apache.maven.archiver.MavenArchiver) IOException(java.io.IOException) ManifestException(org.codehaus.plexus.archiver.jar.ManifestException) File(java.io.File)

Example 22 with ArchiverException

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

the class AbstractWarPackagingTask method copyFile.

/**
     * Copy file from source to destination. The directories up to <code>destination</code> will be created if they
     * don't already exist. if the <code>onlyIfModified</code> flag is <tt>false</tt>, <code>destination</code> will be
     * overwritten if it already exists. If the flag is <tt>true</tt> destination will be overwritten if it's not up to
     * date.
     *
     * @param context the packaging context
     * @param source an existing non-directory <code>File</code> to copy bytes from
     * @param destination a non-directory <code>File</code> to write bytes to (possibly overwriting).
     * @param targetFilename the relative path of the file from the webapp root directory
     * @param onlyIfModified if true, copy the file only if the source has changed, always copy otherwise
     * @return true if the file has been copied/updated, false otherwise
     * @throws IOException if <code>source</code> does not exist, <code>destination</code> cannot be written to, or an
     *             IO error occurs during copying
     */
protected boolean copyFile(WarPackagingContext context, File source, File destination, String targetFilename, boolean onlyIfModified) throws IOException {
    if (onlyIfModified && destination.lastModified() >= source.lastModified()) {
        context.getLog().debug(" * " + targetFilename + " is up to date.");
        return false;
    } else {
        if (source.isDirectory()) {
            context.getLog().warn(" + " + targetFilename + " is packaged from the source folder");
            try {
                JarArchiver archiver = context.getJarArchiver();
                archiver.addDirectory(source);
                archiver.setDestFile(destination);
                archiver.createArchive();
            } catch (ArchiverException e) {
                String msg = "Failed to create " + targetFilename;
                context.getLog().error(msg, e);
                IOException ioe = new IOException(msg);
                ioe.initCause(e);
                throw ioe;
            }
        } else {
            FileUtils.copyFile(source.getCanonicalFile(), destination);
            // preserve timestamp
            destination.setLastModified(source.lastModified());
            context.getLog().debug(" + " + targetFilename + " has been copied.");
        }
        return true;
    }
}
Also used : NoSuchArchiverException(org.codehaus.plexus.archiver.manager.NoSuchArchiverException) ArchiverException(org.codehaus.plexus.archiver.ArchiverException) IOException(java.io.IOException) JarArchiver(org.codehaus.plexus.archiver.jar.JarArchiver)

Example 23 with ArchiverException

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

the class ClassesPackager method packageClasses.

/**
     * Package the classes
     *
     * @param classesDirectory the classes directory
     * @param targetFile the target file
     * @param jarArchiver the jar archiver to use
     * @param session the current session
     * @param project the related project
     * @param archiveConfiguration the archive configuration to use
     * @throws MojoExecutionException if an error occurred while creating the archive
     */
public void packageClasses(File classesDirectory, File targetFile, JarArchiver jarArchiver, MavenSession session, MavenProject project, MavenArchiveConfiguration archiveConfiguration) throws MojoExecutionException {
    try {
        final MavenArchiver archiver = new MavenArchiver();
        archiver.setArchiver(jarArchiver);
        archiver.setOutputFile(targetFile);
        archiver.getArchiver().addDirectory(classesDirectory);
        archiver.createArchive(session, project, archiveConfiguration);
    } catch (ArchiverException e) {
        throw new MojoExecutionException("Could not create classes archive", e);
    } catch (ManifestException e) {
        throw new MojoExecutionException("Could not create classes archive", e);
    } catch (IOException e) {
        throw new MojoExecutionException("Could not create classes archive", e);
    } catch (DependencyResolutionRequiredException e) {
        throw new MojoExecutionException("Could not create classes archive", e);
    }
}
Also used : DependencyResolutionRequiredException(org.apache.maven.artifact.DependencyResolutionRequiredException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ArchiverException(org.codehaus.plexus.archiver.ArchiverException) MavenArchiver(org.apache.maven.archiver.MavenArchiver) IOException(java.io.IOException) ManifestException(org.codehaus.plexus.archiver.jar.ManifestException)

Example 24 with ArchiverException

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

the class AbstractWarPackagingTask method doUnpack.

/**
     * Unpacks the specified file to the specified directory.
     *
     * @param context the packaging context
     * @param file the file to unpack
     * @param unpackDirectory the directory to use for th unpacked file
     * @throws MojoExecutionException if an error occurred while unpacking the file
     */
protected void doUnpack(WarPackagingContext context, File file, File unpackDirectory) throws MojoExecutionException {
    String archiveExt = FileUtils.getExtension(file.getAbsolutePath()).toLowerCase();
    try {
        UnArchiver unArchiver = context.getArchiverManager().getUnArchiver(archiveExt);
        unArchiver.setSourceFile(file);
        unArchiver.setUseJvmChmod(context.isUseJvmChmod());
        unArchiver.setDestDirectory(unpackDirectory);
        unArchiver.setOverwrite(true);
        unArchiver.extract();
    } catch (ArchiverException e) {
        throw new MojoExecutionException("Error unpacking file [" + file.getAbsolutePath() + "]" + " to [" + unpackDirectory.getAbsolutePath() + "]", e);
    } catch (NoSuchArchiverException e) {
        context.getLog().warn("Skip unpacking dependency file [" + file.getAbsolutePath() + " with unknown extension [" + archiveExt + "]");
    }
}
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)

Example 25 with ArchiverException

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

Aggregations

ArchiverException (org.codehaus.plexus.archiver.ArchiverException)50 File (java.io.File)33 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)25 IOException (java.io.IOException)24 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