Search in sources :

Example 6 with JarArchiver

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

the class DefaultAssemblyArchiver method createArchiver.

/**
 * Creates the necessary archiver to build the distribution file.
 *
 * @param format                Archive format
 * @param includeBaseDir        the base directory for include.
 * @param finalName             The final name.
 * @param configSource          {@link AssemblerConfigurationSource}
 * @param containerHandlers     The list of {@link ContainerDescriptorHandler}
 * @param recompressZippedFiles recompress zipped files.
 * @param mergeManifestMode     how to handle already existing Manifest files
 * @return archiver Archiver generated
 * @throws org.codehaus.plexus.archiver.ArchiverException
 * @throws org.codehaus.plexus.archiver.manager.NoSuchArchiverException
 */
protected Archiver createArchiver(final String format, final boolean includeBaseDir, final String finalName, final AssemblerConfigurationSource configSource, final List<ContainerDescriptorHandler> containerHandlers, boolean recompressZippedFiles, String mergeManifestMode) throws NoSuchArchiverException {
    Archiver archiver;
    if ("txz".equals(format) || "tgz".equals(format) || "tbz2".equals(format) || format.startsWith("tar")) {
        archiver = createTarArchiver(format, TarLongFileMode.valueOf(configSource.getTarLongFileMode()));
    } else if ("war".equals(format)) {
        archiver = createWarArchiver();
    } else {
        archiver = archiverManager.getArchiver(format);
    }
    if (archiver instanceof AbstractZipArchiver) {
        ((AbstractZipArchiver) archiver).setRecompressAddedZips(recompressZippedFiles);
    }
    final List<FileSelector> extraSelectors = new ArrayList<FileSelector>();
    final List<ArchiveFinalizer> extraFinalizers = new ArrayList<ArchiveFinalizer>();
    if (archiver instanceof JarArchiver) {
        if (mergeManifestMode != null) {
            ((JarArchiver) archiver).setFilesetmanifest(JarArchiver.FilesetManifestConfig.valueOf(mergeManifestMode));
        }
        extraSelectors.add(new JarSecurityFileSelector());
        extraFinalizers.add(new ManifestCreationFinalizer(configSource.getMavenSession(), configSource.getProject(), configSource.getJarArchiveConfiguration()));
    }
    if (configSource.getArchiverConfig() != null) {
        configureArchiver(archiver, configSource);
    }
    String prefix = "";
    if (includeBaseDir) {
        prefix = finalName;
    }
    archiver = new AssemblyProxyArchiver(prefix, archiver, containerHandlers, extraSelectors, extraFinalizers, configSource.getWorkingDirectory(), getLogger());
    if (configSource.isDryRun()) {
        archiver = new DryRunArchiver(archiver, getLogger());
    }
    archiver.setUseJvmChmod(configSource.isUpdateOnly());
    archiver.setIgnorePermissions(configSource.isIgnorePermissions());
    archiver.setForced(!configSource.isUpdateOnly());
    return archiver;
}
Also used : AssemblyProxyArchiver(org.apache.maven.plugins.assembly.archive.archiver.AssemblyProxyArchiver) FileSelector(org.codehaus.plexus.components.io.fileselectors.FileSelector) JarSecurityFileSelector(org.codehaus.plexus.archiver.filters.JarSecurityFileSelector) ArrayList(java.util.ArrayList) JarSecurityFileSelector(org.codehaus.plexus.archiver.filters.JarSecurityFileSelector) DryRunArchiver(org.codehaus.plexus.archiver.diags.DryRunArchiver) ArchiveFinalizer(org.codehaus.plexus.archiver.ArchiveFinalizer) AssemblyProxyArchiver(org.apache.maven.plugins.assembly.archive.archiver.AssemblyProxyArchiver) AbstractZipArchiver(org.codehaus.plexus.archiver.zip.AbstractZipArchiver) JarArchiver(org.codehaus.plexus.archiver.jar.JarArchiver) TarArchiver(org.codehaus.plexus.archiver.tar.TarArchiver) Archiver(org.codehaus.plexus.archiver.Archiver) DryRunArchiver(org.codehaus.plexus.archiver.diags.DryRunArchiver) WarArchiver(org.codehaus.plexus.archiver.war.WarArchiver) JarArchiver(org.codehaus.plexus.archiver.jar.JarArchiver) AbstractZipArchiver(org.codehaus.plexus.archiver.zip.AbstractZipArchiver)

Example 7 with JarArchiver

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

the class ManifestCreationFinalizer method finalizeArchiveCreation.

@Override
public void finalizeArchiveCreation(final Archiver archiver) {
    if (archiveConfiguration != null) {
        try {
            Manifest manifest;
            final File manifestFile = archiveConfiguration.getManifestFile();
            if (manifestFile != null) {
                Reader manifestFileReader = null;
                try {
                    manifestFileReader = new InputStreamReader(new FileInputStream(manifestFile), "UTF-8");
                    manifest = new Manifest(manifestFileReader);
                    manifestFileReader.close();
                    manifestFileReader = null;
                } catch (final FileNotFoundException e) {
                    throw new ArchiverException("Manifest not found: " + e.getMessage(), e);
                } catch (final IOException e) {
                    throw new ArchiverException("Error processing manifest: " + e.getMessage(), e);
                } finally {
                    IOUtil.close(manifestFileReader);
                }
            } else {
                manifest = mavenArchiver.getManifest(session, project, archiveConfiguration);
            }
            if ((manifest != null) && (archiver instanceof JarArchiver)) {
                final JarArchiver jarArchiver = (JarArchiver) archiver;
                jarArchiver.addConfiguredManifest(manifest);
            }
        } catch (final ManifestException e) {
            throw new ArchiverException("Error creating manifest: " + e.getMessage(), e);
        } catch (final DependencyResolutionRequiredException e) {
            throw new ArchiverException("Dependencies were not resolved: " + e.getMessage(), e);
        }
    }
}
Also used : DependencyResolutionRequiredException(org.apache.maven.artifact.DependencyResolutionRequiredException) InputStreamReader(java.io.InputStreamReader) ArchiverException(org.codehaus.plexus.archiver.ArchiverException) FileNotFoundException(java.io.FileNotFoundException) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) Manifest(org.codehaus.plexus.archiver.jar.Manifest) ManifestException(org.codehaus.plexus.archiver.jar.ManifestException) File(java.io.File) JarArchiver(org.codehaus.plexus.archiver.jar.JarArchiver) FileInputStream(java.io.FileInputStream)

Example 8 with JarArchiver

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

the class ManifestCreationFinalizerTest method testShouldAddManifestWhenArchiverIsJarArchiver.

public void testShouldAddManifestWhenArchiverIsJarArchiver() throws ArchiverException, IOException {
    MavenProject project = new MavenProject(new Model());
    MavenArchiveConfiguration config = new MavenArchiveConfiguration();
    File tempDir = fileManager.createTempDir();
    File manifestFile = fileManager.createFile(tempDir, "MANIFEST.MF", "Main-Class: Stuff\n");
    config.setManifestFile(manifestFile);
    JarArchiver archiver = new JarArchiver();
    archiver.setArchiveFinalizers(Collections.<ArchiveFinalizer>singletonList(new ManifestCreationFinalizer(null, project, config)));
    File file = fileManager.createTempFile();
    archiver.setDestFile(file);
    archiver.createArchive();
    URL resource = new URL("jar:file:" + file.getAbsolutePath() + "!/META-INF/MANIFEST.MF");
    BufferedReader reader = new BufferedReader(new InputStreamReader(resource.openStream()));
    StringWriter writer = new StringWriter();
    IOUtil.copy(reader, writer);
    assertTrue(writer.toString().contains("Main-Class: Stuff"));
    // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4823678
    ((JarURLConnection) resource.openConnection()).getJarFile().close();
}
Also used : MavenProject(org.apache.maven.project.MavenProject) InputStreamReader(java.io.InputStreamReader) StringWriter(java.io.StringWriter) Model(org.apache.maven.model.Model) BufferedReader(java.io.BufferedReader) MavenArchiveConfiguration(org.apache.maven.archiver.MavenArchiveConfiguration) File(java.io.File) JarArchiver(org.codehaus.plexus.archiver.jar.JarArchiver) URL(java.net.URL)

Example 9 with JarArchiver

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

the class ManifestCreationFinalizerTest method testShouldAddManifestEntriesWhenArchiverIsJarArchiver.

public void testShouldAddManifestEntriesWhenArchiverIsJarArchiver() throws ArchiverException, IOException {
    MavenProject project = new MavenProject(new Model());
    MavenArchiveConfiguration config = new MavenArchiveConfiguration();
    String testKey = "Test-Key";
    String testValue = "test-value";
    config.addManifestEntry(testKey, testValue);
    JarArchiver archiver = new JarArchiver();
    archiver.setArchiveFinalizers(Collections.<ArchiveFinalizer>singletonList(new ManifestCreationFinalizer(null, project, config)));
    File file = fileManager.createTempFile();
    archiver.setDestFile(file);
    archiver.createArchive();
    URL resource = new URL("jar:file:" + file.getAbsolutePath() + "!/META-INF/MANIFEST.MF");
    BufferedReader reader = new BufferedReader(new InputStreamReader(resource.openStream()));
    StringWriter writer = new StringWriter();
    IOUtil.copy(reader, writer);
    System.out.println("Test Manifest:\n\n" + writer);
    assertTrue(writer.toString().contains(testKey + ": " + testValue));
    // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4823678
    ((JarURLConnection) resource.openConnection()).getJarFile().close();
}
Also used : MavenProject(org.apache.maven.project.MavenProject) InputStreamReader(java.io.InputStreamReader) StringWriter(java.io.StringWriter) Model(org.apache.maven.model.Model) BufferedReader(java.io.BufferedReader) MavenArchiveConfiguration(org.apache.maven.archiver.MavenArchiveConfiguration) JarArchiver(org.codehaus.plexus.archiver.jar.JarArchiver) File(java.io.File) URL(java.net.URL)

Example 10 with JarArchiver

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

the class EarMojo method execute.

/**
 * {@inheritDoc}
 */
public void execute() throws MojoExecutionException, MojoFailureException {
    // Initializes ear modules
    super.execute();
    zipArchiver.setUseJvmChmod(useJvmChmod);
    zipUnArchiver.setUseJvmChmod(useJvmChmod);
    final JavaEEVersion javaEEVersion = JavaEEVersion.getJavaEEVersion(version);
    // Initializes unpack types
    List<String> unpackTypesList = createUnpackList();
    // Copy modules
    copyModules(javaEEVersion, unpackTypesList);
    // Copy source files
    try {
        File earSourceDir = earSourceDirectory;
        if (earSourceDir.exists()) {
            getLog().info("Copy ear sources to " + getWorkDirectory().getAbsolutePath());
            String[] fileNames = getEarFiles(earSourceDir);
            for (String fileName : fileNames) {
                copyFile(new File(earSourceDir, fileName), new File(getWorkDirectory(), fileName));
            }
        }
        if (applicationXml != null && !"".equals(applicationXml)) {
            // rename to application.xml
            getLog().info("Including custom application.xml[" + applicationXml + "]");
            File metaInfDir = new File(getWorkDirectory(), META_INF);
            copyFile(new File(applicationXml), new File(metaInfDir, "/application.xml"));
        }
    } catch (IOException e) {
        throw new MojoExecutionException("Error copying EAR sources", e);
    } catch (MavenFilteringException e) {
        throw new MojoExecutionException("Error filtering EAR sources", e);
    }
    // Check if deployment descriptor is there
    File ddFile = new File(getWorkDirectory(), APPLICATION_XML_URI);
    if (!ddFile.exists() && (javaEEVersion.lt(JavaEEVersion.FIVE))) {
        // CHECKSTYLE_OFF: LineLength
        throw new MojoExecutionException("Deployment descriptor: " + ddFile.getAbsolutePath() + " does not exist.");
    // CHECKSTYLE_ON: LineLength
    }
    try {
        File earFile = getEarFile(outputDirectory, finalName, classifier);
        final MavenArchiver archiver = new EarMavenArchiver(getModules());
        final JarArchiver theJarArchiver = getJarArchiver();
        getLog().debug("Jar archiver implementation [" + theJarArchiver.getClass().getName() + "]");
        archiver.setArchiver(theJarArchiver);
        archiver.setOutputFile(earFile);
        getLog().debug("Excluding " + Arrays.asList(getPackagingExcludes()) + " from the generated EAR.");
        getLog().debug("Including " + Arrays.asList(getPackagingIncludes()) + " in the generated EAR.");
        archiver.getArchiver().addDirectory(getWorkDirectory(), getPackagingIncludes(), getPackagingExcludes());
        archiver.createArchive(session, getProject(), archive);
        if (classifier != null) {
            projectHelper.attachArtifact(getProject(), "ear", classifier, earFile);
        } else {
            getProject().getArtifact().setFile(earFile);
        }
    } catch (Exception e) {
        throw new MojoExecutionException("Error assembling EAR", e);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MavenFilteringException(org.apache.maven.shared.filtering.MavenFilteringException) JavaEEVersion(org.apache.maven.plugins.ear.util.JavaEEVersion) EarMavenArchiver(org.apache.maven.plugins.ear.util.EarMavenArchiver) MavenArchiver(org.apache.maven.archiver.MavenArchiver) IOException(java.io.IOException) EarMavenArchiver(org.apache.maven.plugins.ear.util.EarMavenArchiver) File(java.io.File) JarArchiver(org.codehaus.plexus.archiver.jar.JarArchiver) ManifestException(org.codehaus.plexus.archiver.jar.ManifestException) NoSuchArchiverException(org.codehaus.plexus.archiver.manager.NoSuchArchiverException) ArchiverException(org.codehaus.plexus.archiver.ArchiverException) MavenFilteringException(org.apache.maven.shared.filtering.MavenFilteringException) ZipException(java.util.zip.ZipException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException)

Aggregations

JarArchiver (org.codehaus.plexus.archiver.jar.JarArchiver)14 File (java.io.File)11 IOException (java.io.IOException)9 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)6 ArchiverException (org.codehaus.plexus.archiver.ArchiverException)5 Manifest (org.codehaus.plexus.archiver.jar.Manifest)5 ManifestException (org.codehaus.plexus.archiver.jar.ManifestException)4 InputStreamReader (java.io.InputStreamReader)3 URI (java.net.URI)3 MavenArchiveConfiguration (org.apache.maven.archiver.MavenArchiveConfiguration)3 Archiver (org.codehaus.plexus.archiver.Archiver)3 Attribute (org.codehaus.plexus.archiver.jar.Manifest.Attribute)3 BufferedReader (java.io.BufferedReader)2 StringWriter (java.io.StringWriter)2 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2 MavenArchiver (org.apache.maven.archiver.MavenArchiver)2 DependencyResolutionRequiredException (org.apache.maven.artifact.DependencyResolutionRequiredException)2 AbstractArtifactResolutionException (org.apache.maven.artifact.resolver.AbstractArtifactResolutionException)2 Model (org.apache.maven.model.Model)2