Search in sources :

Example 6 with Manifest

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

the class EarMavenArchiver method getManifest.

/** {@inheritDoc} */
public Manifest getManifest(MavenSession session, MavenProject project, MavenArchiveConfiguration config) throws ManifestException, DependencyResolutionRequiredException {
    final Manifest manifest = super.getManifest(session, project, config);
    if (config.getManifest().isAddClasspath()) {
        String earManifestClassPathEntry = generateClassPathEntry(config.getManifest().getClasspathPrefix());
        // Class-path can be customized. Let's make sure we don't overwrite this
        // with our custom change!
        final String userSuppliedClassPathEntry = getUserSuppliedClassPathEntry(config);
        if (userSuppliedClassPathEntry != null) {
            earManifestClassPathEntry = userSuppliedClassPathEntry + " " + earManifestClassPathEntry;
        }
        // Overwrite the existing one, if any
        final Manifest.Attribute classPathAttr = manifest.getMainSection().getAttribute(CLASS_PATH_KEY);
        if (classPathAttr != null) {
            classPathAttr.setValue(earManifestClassPathEntry);
        } else {
            final Manifest.Attribute attr = new Manifest.Attribute(CLASS_PATH_KEY, earManifestClassPathEntry);
            manifest.addConfiguredAttribute(attr);
        }
    }
    return manifest;
}
Also used : Manifest(org.codehaus.plexus.archiver.jar.Manifest)

Example 7 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 8 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)

Example 9 with Manifest

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

the class ExecMojo method createExecutableJar.

private void createExecutableJar() throws Exception {
    mkdirs(execFile.getParentFile());
    final Properties config = new Properties();
    config.put("distribution", distributionName);
    config.put("workingDir", runtimeWorkingDir);
    config.put("command", DEFAULT_SCRIPT.equals(script) ? (skipArchiveRootFolder ? "" : catalinaBase.getName() + "/") + DEFAULT_SCRIPT : script);
    final List<String> jvmArgs = generateJVMArgs();
    final String catalinaOpts = toString(jvmArgs, " ");
    config.put("catalinaOpts", catalinaOpts);
    config.put("timestamp", Long.toString(System.currentTimeMillis()));
    // java only
    final String cp = getAdditionalClasspath();
    if (cp != null) {
        config.put("additionalClasspath", cp);
    }
    config.put("shutdownCommand", tomeeShutdownCommand);
    int i = 0;
    boolean encodingSet = catalinaOpts.contains("-Dfile.encoding");
    for (final String jvmArg : jvmArgs) {
        config.put("jvmArg." + i++, jvmArg);
        encodingSet = encodingSet || jvmArg.contains("-Dfile.encoding");
    }
    if (!encodingSet) {
        // forcing encoding for launched process to be able to read conf files
        config.put("jvmArg." + i, "-Dfile.encoding=UTF-8");
    }
    if (preTasks != null) {
        config.put("preTasks", toString(preTasks, ","));
    }
    if (postTasks != null) {
        config.put("postTasks", toString(postTasks, ","));
    }
    config.put("waitFor", Boolean.toString(waitFor));
    // create an executable jar with main runner and zipFile
    final FileOutputStream fileOutputStream = new FileOutputStream(execFile);
    final ArchiveOutputStream os = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.JAR, fileOutputStream);
    {
        // distrib
        os.putArchiveEntry(new JarArchiveEntry(distributionName));
        final FileInputStream in = new FileInputStream(zipFile);
        try {
            IOUtils.copy(in, os);
            os.closeArchiveEntry();
        } finally {
            IOUtil.close(in);
        }
    }
    {
        // config
        os.putArchiveEntry(new JarArchiveEntry("configuration.properties"));
        final StringWriter writer = new StringWriter();
        config.store(writer, "");
        IOUtils.copy(new ByteArrayInputStream(writer.toString().getBytes("UTF-8")), os);
        os.closeArchiveEntry();
    }
    {
        // Manifest
        final Manifest manifest = new Manifest();
        final Manifest.Attribute mainClassAtt = new Manifest.Attribute();
        mainClassAtt.setName("Main-Class");
        mainClassAtt.setValue(runnerClass);
        manifest.addConfiguredAttribute(mainClassAtt);
        final ByteArrayOutputStream baos = new ByteArrayOutputStream(512);
        manifest.write(baos);
        os.putArchiveEntry(new JarArchiveEntry("META-INF/MANIFEST.MF"));
        IOUtils.copy(new ByteArrayInputStream(baos.toByteArray()), os);
        os.closeArchiveEntry();
    }
    {
        // Main + utility
        for (final Class<?> clazz : asList(ExecRunner.class, Files.class, Files.PatternFileFilter.class, Files.DeleteThread.class, Files.FileRuntimeException.class, Files.FileDoesNotExistException.class, Files.NoopOutputStream.class, LoaderRuntimeException.class, Pipe.class, IO.class, Zips.class, JarLocation.class, RemoteServer.class, RemoteServer.CleanUpThread.class, OpenEJBRuntimeException.class, Join.class, QuickServerXmlParser.class, Options.class, Options.NullLog.class, Options.TomEEPropertyAdapter.class, Options.NullOptions.class, Options.Log.class, JavaSecurityManagers.class)) {
            addToJar(os, clazz);
        }
    }
    addClasses(additionalClasses, os);
    addClasses(preTasks, os);
    addClasses(postTasks, os);
    IOUtil.close(os);
    IOUtil.close(fileOutputStream);
}
Also used : Options(org.apache.openejb.loader.Options) JarLocation(org.apache.openejb.loader.JarLocation) Properties(java.util.Properties) ExecRunner(org.apache.openejb.maven.plugin.runner.ExecRunner) StringWriter(java.io.StringWriter) LoaderRuntimeException(org.apache.openejb.loader.LoaderRuntimeException) JavaSecurityManagers(org.apache.openejb.util.JavaSecurityManagers) Files(org.apache.openejb.loader.Files) RemoteServer(org.apache.openejb.config.RemoteServer) JarArchiveEntry(org.apache.commons.compress.archivers.jar.JarArchiveEntry) IO(org.apache.openejb.loader.IO) Join(org.apache.openejb.util.Join) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Pipe(org.apache.openejb.util.Pipe) Manifest(org.codehaus.plexus.archiver.jar.Manifest) FileInputStream(java.io.FileInputStream) ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) QuickServerXmlParser(org.apache.tomee.util.QuickServerXmlParser) ByteArrayInputStream(java.io.ByteArrayInputStream) FileOutputStream(java.io.FileOutputStream) Zips(org.apache.openejb.loader.Zips) ArchiveOutputStream(org.apache.commons.compress.archivers.ArchiveOutputStream)

Example 10 with Manifest

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

the class ManifestBuilder method getUserSuppliedManifest.

@Nullable
private Manifest getUserSuppliedManifest(@Nullable Element mavenArchiveConfiguration) {
    Manifest manifest = null;
    String manifestPath = MavenJDOMUtil.findChildValueByPath(mavenArchiveConfiguration, "manifestFile");
    if (manifestPath != null) {
        File manifestFile = new File(manifestPath);
        if (!manifestFile.isAbsolute()) {
            manifestFile = new File(myMavenProject.getDirectory(), manifestPath);
        }
        if (manifestFile.isFile()) {
            FileInputStream fis = null;
            try {
                //noinspection IOResourceOpenedButNotSafelyClosed
                fis = new FileInputStream(manifestFile);
                manifest = new Manifest(fis);
            } catch (IOException ignore) {
            } finally {
                StreamUtil.closeStream(fis);
            }
        }
    }
    return manifest;
}
Also used : IOException(java.io.IOException) Manifest(org.codehaus.plexus.archiver.jar.Manifest) File(java.io.File) FileInputStream(java.io.FileInputStream) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

Manifest (org.codehaus.plexus.archiver.jar.Manifest)13 File (java.io.File)6 IOException (java.io.IOException)5 Attribute (org.codehaus.plexus.archiver.jar.Manifest.Attribute)5 FileInputStream (java.io.FileInputStream)4 ManifestException (org.codehaus.plexus.archiver.jar.ManifestException)4 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)3 JarArchiver (org.codehaus.plexus.archiver.jar.JarArchiver)3 NotNull (org.jetbrains.annotations.NotNull)3 PrintWriter (java.io.PrintWriter)2 URI (java.net.URI)2 Map (java.util.Map)2 ArchiverException (org.codehaus.plexus.archiver.ArchiverException)2 CommandLineException (org.codehaus.plexus.util.cli.CommandLineException)2 Commandline (org.codehaus.plexus.util.cli.Commandline)2 Element (org.jdom.Element)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1