Search in sources :

Example 1 with Manifest

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

the class ManifestBuilder method build.

@NotNull
public java.util.jar.Manifest build() throws ManifestBuilderException {
    try {
        Element mavenPackagingPluginConfiguration = getMavenPackagingPluginConfiguration(myMavenProject);
        final Element mavenArchiveConfiguration = mavenPackagingPluginConfiguration != null ? mavenPackagingPluginConfiguration.getChild("archive") : null;
        if (mavenArchiveConfiguration == null)
            return getDefaultManifest(Collections.emptyMap());
        final Element manifestEntries = mavenArchiveConfiguration.getChild("manifestEntries");
        Map<String, String> entries = getManifestEntries(manifestEntries);
        final Element manifestConfiguration = mavenArchiveConfiguration.getChild("manifest");
        final Manifest configuredManifest = getConfiguredManifest(myMavenProject, manifestConfiguration, entries);
        if (!entries.isEmpty()) {
            addManifestEntries(configuredManifest, entries);
        }
        addCustomManifestSections(configuredManifest, mavenArchiveConfiguration);
        Manifest finalManifest = getDefaultManifest(entries);
        // merge configured manifest
        merge(finalManifest, configuredManifest);
        // merge user supplied manifest
        final Manifest userSuppliedManifest = getUserSuppliedManifest(mavenArchiveConfiguration);
        merge(finalManifest, userSuppliedManifest);
        return finalManifest;
    } catch (ManifestException e) {
        throw new ManifestBuilderException(e);
    }
}
Also used : Element(org.jdom.Element) Manifest(org.codehaus.plexus.archiver.jar.Manifest) ManifestException(org.codehaus.plexus.archiver.jar.ManifestException) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with Manifest

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

the class ManifestBuilder method getConfiguredManifest.

@NotNull
private static Manifest getConfiguredManifest(@NotNull MavenProject mavenProject, @Nullable Element manifestConfiguration, @NotNull Map<String, String> entries) throws ManifestException {
    final Manifest manifest = new Manifest();
    boolean isAddDefaultSpecificationEntries = Boolean.valueOf(MavenJDOMUtil.findChildValueByPath(manifestConfiguration, "addDefaultSpecificationEntries", "false"));
    if (isAddDefaultSpecificationEntries) {
        addManifestAttribute(manifest, entries, "Specification-Title", mavenProject.getName());
        addManifestAttribute(manifest, entries, "Specification-Version", mavenProject.getMavenId().getVersion());
    }
    boolean isAddDefaultImplementationEntries = Boolean.valueOf(MavenJDOMUtil.findChildValueByPath(manifestConfiguration, "addDefaultImplementationEntries", "false"));
    if (isAddDefaultImplementationEntries) {
        addManifestAttribute(manifest, entries, "Implementation-Title", mavenProject.getName());
        addManifestAttribute(manifest, entries, "Implementation-Version", mavenProject.getMavenId().getVersion());
        addManifestAttribute(manifest, entries, "Implementation-Vendor-Id", mavenProject.getMavenId().getGroupId());
    }
    String packageName = MavenJDOMUtil.findChildValueByPath(manifestConfiguration, "packageName");
    if (packageName != null) {
        addManifestAttribute(manifest, entries, "Package", packageName);
    }
    String mainClass = MavenJDOMUtil.findChildValueByPath(manifestConfiguration, "mainClass");
    if (!StringUtil.isEmpty(mainClass)) {
        addManifestAttribute(manifest, entries, "Main-Class", mainClass);
    }
    boolean isAddClasspath = Boolean.valueOf(MavenJDOMUtil.findChildValueByPath(manifestConfiguration, "addClasspath", "false"));
    if (isAddClasspath) {
        final ManifestImporter manifestImporter = ManifestImporter.getManifestImporter(mavenProject.getPackaging());
        String classpath = manifestImporter.getClasspath(mavenProject, manifestConfiguration);
        if (!classpath.isEmpty()) {
            addManifestAttribute(manifest, "Class-Path", classpath);
        }
    }
    return manifest;
}
Also used : Manifest(org.codehaus.plexus.archiver.jar.Manifest) ManifestImporter(org.jetbrains.idea.maven.importing.ManifestImporter) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with Manifest

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

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

Example 5 with Manifest

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

the class AbstractCodegenMoho method runForked.

protected void runForked(Set<URI> classPath, String mainClassName, String[] args) throws MojoExecutionException {
    getLog().info("Running code generation in fork mode...");
    getLog().debug("Running code generation in fork mode with args " + Arrays.asList(args));
    Commandline cmd = new Commandline();
    // for JVM args
    cmd.getShell().setQuotedArgumentsEnabled(true);
    cmd.setWorkingDirectory(project.getBuild().getDirectory());
    try {
        cmd.setExecutable(getJavaExecutable().getAbsolutePath());
    } catch (IOException e) {
        getLog().debug(e);
        throw new MojoExecutionException(e.getMessage(), e);
    }
    cmd.createArg().setLine(additionalJvmArgs);
    File file = null;
    try {
        // file = new File("/tmp/test.jar");
        file = FileUtils.createTempFile("cxf-codegen", ".jar");
        JarArchiver jar = new JarArchiver();
        jar.setDestFile(file.getAbsoluteFile());
        Manifest manifest = new Manifest();
        Attribute attr = new Attribute();
        attr.setName("Class-Path");
        StringBuilder b = new StringBuilder(8000);
        for (URI cp : classPath) {
            b.append(cp.toURL().toExternalForm()).append(' ');
        }
        attr.setValue(b.toString());
        manifest.getMainSection().addConfiguredAttribute(attr);
        attr = new Attribute();
        attr.setName("Main-Class");
        attr.setValue(mainClassName);
        manifest.getMainSection().addConfiguredAttribute(attr);
        jar.addConfiguredManifest(manifest);
        jar.createArchive();
        cmd.createArg().setValue("-jar");
        String tmpFilePath = file.getAbsolutePath();
        if (tmpFilePath.contains(" ")) {
            // ensure the path is in double quotation marks if the path contain space
            tmpFilePath = "\"" + tmpFilePath + "\"";
        }
        cmd.createArg().setValue(tmpFilePath);
    } catch (Exception e1) {
        throw new MojoExecutionException("Could not create runtime jar", e1);
    }
    cmd.addArguments(args);
    StreamConsumer out = new StreamConsumer() {

        public void consumeLine(String line) {
            getLog().info(line);
        }
    };
    final StringBuilder b = new StringBuilder();
    StreamConsumer err = new StreamConsumer() {

        public void consumeLine(String line) {
            b.append(line);
            b.append("\n");
            getLog().warn(line);
        }
    };
    int exitCode;
    try {
        exitCode = CommandLineUtils.executeCommandLine(cmd, out, err);
    } catch (CommandLineException e) {
        getLog().debug(e);
        throw new MojoExecutionException(e.getMessage(), e);
    }
    String cmdLine = CommandLineUtils.toString(cmd.getCommandline());
    if (exitCode != 0) {
        StringBuilder msg = new StringBuilder("\nExit code: ");
        msg.append(exitCode);
        msg.append('\n');
        msg.append("Command line was: ").append(cmdLine).append('\n').append('\n');
        throw new MojoExecutionException(msg.toString());
    }
    file.delete();
    if (b.toString().contains("WSDL2Java Error")) {
        StringBuilder msg = new StringBuilder();
        msg.append(b.toString());
        msg.append('\n');
        msg.append("Command line was: ").append(cmdLine).append('\n').append('\n');
        throw new MojoExecutionException(msg.toString());
    }
}
Also used : StreamConsumer(org.codehaus.plexus.util.cli.StreamConsumer) Commandline(org.codehaus.plexus.util.cli.Commandline) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Attribute(org.codehaus.plexus.archiver.jar.Manifest.Attribute) IOException(java.io.IOException) Manifest(org.codehaus.plexus.archiver.jar.Manifest) URI(java.net.URI) CommandLineException(org.codehaus.plexus.util.cli.CommandLineException) AbstractArtifactResolutionException(org.apache.maven.artifact.resolver.AbstractArtifactResolutionException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) CommandLineException(org.codehaus.plexus.util.cli.CommandLineException) File(java.io.File) JarArchiver(org.codehaus.plexus.archiver.jar.JarArchiver)

Aggregations

Manifest (org.codehaus.plexus.archiver.jar.Manifest)15 File (java.io.File)8 IOException (java.io.IOException)6 Attribute (org.codehaus.plexus.archiver.jar.Manifest.Attribute)6 JarArchiver (org.codehaus.plexus.archiver.jar.JarArchiver)5 ManifestException (org.codehaus.plexus.archiver.jar.ManifestException)5 FileInputStream (java.io.FileInputStream)4 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)4 URI (java.net.URI)3 CommandLineException (org.codehaus.plexus.util.cli.CommandLineException)3 PrintWriter (java.io.PrintWriter)2 Map (java.util.Map)2 AbstractArtifactResolutionException (org.apache.maven.artifact.resolver.AbstractArtifactResolutionException)2 ArchiverException (org.codehaus.plexus.archiver.ArchiverException)2 Commandline (org.codehaus.plexus.util.cli.Commandline)2 StreamConsumer (org.codehaus.plexus.util.cli.StreamConsumer)2 Element (org.jdom.Element)2 NotNull (org.jetbrains.annotations.NotNull)2 AzureToolkitRuntimeException (com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1