Search in sources :

Example 1 with Attribute

use of org.codehaus.plexus.archiver.jar.Manifest.Attribute in project intellij-community by JetBrains.

the class ManifestBuilder method addManifestAttribute.

private static void addManifestAttribute(@NotNull Manifest manifest, String key, String value) throws ManifestException {
    if (!StringUtil.isEmpty(value)) {
        Attribute attr = new Attribute(key, value);
        manifest.addConfiguredAttribute(attr);
    }
}
Also used : Attribute(org.codehaus.plexus.archiver.jar.Manifest.Attribute)

Example 2 with Attribute

use of org.codehaus.plexus.archiver.jar.Manifest.Attribute in project intellij-community by JetBrains.

the class ManifestBuilder method addCustomManifestSections.

private static void addCustomManifestSections(@NotNull Manifest manifest, @NotNull Element mavenArchiveConfiguration) throws ManifestException {
    for (Element section : MavenJDOMUtil.findChildrenByPath(mavenArchiveConfiguration, "manifestSections", "manifestSection")) {
        Manifest.Section theSection = new Manifest.Section();
        final String sectionName = MavenJDOMUtil.findChildValueByPath(section, "name");
        theSection.setName(sectionName);
        final Element manifestEntries = section.getChild("manifestEntries");
        Map<String, String> entries = getManifestEntries(manifestEntries);
        if (!entries.isEmpty()) {
            for (Map.Entry<String, String> entry : entries.entrySet()) {
                Attribute attr = new Attribute(entry.getKey(), entry.getValue());
                theSection.addConfiguredAttribute(attr);
            }
        }
        manifest.addConfiguredSection(theSection);
    }
}
Also used : Attribute(org.codehaus.plexus.archiver.jar.Manifest.Attribute) Element(org.jdom.Element) Manifest(org.codehaus.plexus.archiver.jar.Manifest) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 3 with Attribute

use of org.codehaus.plexus.archiver.jar.Manifest.Attribute 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 4 with Attribute

use of org.codehaus.plexus.archiver.jar.Manifest.Attribute in project sling by apache.

the class JarArchiverHelper method createManifest.

/**
     * Create a manifest
     */
private void createManifest(final java.util.jar.Manifest manifest) throws MojoExecutionException {
    // create a new manifest
    final Manifest outManifest = new Manifest();
    try {
        boolean hasMain = false;
        // copy entries from existing manifest
        if (manifest != null) {
            final Map<Object, Object> attrs = manifest.getMainAttributes();
            for (final Map.Entry<Object, Object> entry : attrs.entrySet()) {
                final String key = entry.getKey().toString();
                if (!BuildConstants.ATTRS_EXCLUDES.contains(key)) {
                    final Attribute a = new Attribute(key, entry.getValue().toString());
                    outManifest.addConfiguredAttribute(a);
                }
                if (key.equals(BuildConstants.ATTR_MAIN_CLASS)) {
                    hasMain = true;
                }
            }
        }
        outManifest.addConfiguredAttribute(new Attribute(BuildConstants.ATTR_IMPLEMENTATION_BUILD, project.getVersion()));
        outManifest.addConfiguredAttribute(new Attribute(BuildConstants.ATTR_IMPLEMENTATION_VERSION, project.getVersion()));
        String organizationName = project.getOrganization() != null ? project.getOrganization().getName() : null;
        if (organizationName != null) {
            outManifest.addConfiguredAttribute(new Attribute(BuildConstants.ATTR_IMPLEMENTATION_VENDOR, organizationName));
            outManifest.addConfiguredAttribute(new Attribute(BuildConstants.ATTR_CREATED_BY, organizationName));
            outManifest.addConfiguredAttribute(new Attribute(BuildConstants.ATTR_BUILT_BY, organizationName));
            outManifest.addConfiguredAttribute(new Attribute(BuildConstants.ATTR_SPECIFICATION_VENDOR, organizationName));
        }
        outManifest.addConfiguredAttribute(new Attribute(BuildConstants.ATTR_IMPLEMENTATION_VENDOR_ID, project.getGroupId()));
        outManifest.addConfiguredAttribute(new Attribute(BuildConstants.ATTR_IMPLEMENTATION_TITLE, project.getName()));
        outManifest.addConfiguredAttribute(new Attribute(BuildConstants.ATTR_SPECIFICATION_TITLE, project.getName()));
        outManifest.addConfiguredAttribute(new Attribute(BuildConstants.ATTR_SPECIFICATION_VERSION, project.getVersion()));
        if (archiver.getDestFile().getName().endsWith(".jar") && !hasMain) {
            outManifest.addConfiguredAttribute(new Attribute(BuildConstants.ATTR_MAIN_CLASS, BuildConstants.ATTR_VALUE_MAIN_CLASS));
        }
        archiver.addConfiguredManifest(outManifest);
    } catch (final ManifestException e) {
        throw new MojoExecutionException("Unable to create manifest for " + this.archiver.getDestFile(), e);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Attribute(org.codehaus.plexus.archiver.jar.Manifest.Attribute) Manifest(org.codehaus.plexus.archiver.jar.Manifest) ManifestException(org.codehaus.plexus.archiver.jar.ManifestException) Map(java.util.Map)

Aggregations

Attribute (org.codehaus.plexus.archiver.jar.Manifest.Attribute)4 Manifest (org.codehaus.plexus.archiver.jar.Manifest)3 Map (java.util.Map)2 ManifestException (org.codehaus.plexus.archiver.jar.ManifestException)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 ZipException (java.util.zip.ZipException)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 MojoFailureException (org.apache.maven.plugin.MojoFailureException)1 ArchiverException (org.codehaus.plexus.archiver.ArchiverException)1 NoSuchArchiverException (org.codehaus.plexus.archiver.manager.NoSuchArchiverException)1 Element (org.jdom.Element)1