Search in sources :

Example 1 with JarArchiver

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

use of org.codehaus.plexus.archiver.jar.JarArchiver in project tycho by eclipse.

the class PackagePluginMojo method makeJar.

private File makeJar(BuildOutputJar jar) throws MojoExecutionException {
    String jarName = jar.getName();
    BuildProperties buildProperties = pdeProject.getBuildProperties();
    String customManifest = buildProperties.getJarToManifestMap().get(jarName);
    try {
        File jarFile = new File(project.getBasedir(), jarName);
        JarArchiver archiver = new JarArchiver();
        archiver.setDestFile(jarFile);
        archiver.addDirectory(jar.getOutputDirectory());
        if (customManifest != null) {
            for (File sourceFolder : jar.getSourceFolders()) {
                File manifestFile = new File(sourceFolder, customManifest);
                if (manifestFile.isFile()) {
                    archiver.setManifest(manifestFile);
                    break;
                }
            }
        }
        archiver.createArchive();
        return jarFile;
    } catch (Exception e) {
        throw new MojoExecutionException("Could not create jar " + jarName, e);
    }
}
Also used : BuildProperties(org.eclipse.tycho.core.shared.BuildProperties) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) File(java.io.File) JarArchiver(org.codehaus.plexus.archiver.jar.JarArchiver) ManifestException(org.codehaus.plexus.archiver.jar.ManifestException) ArchiverException(org.codehaus.plexus.archiver.ArchiverException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) DependencyResolutionRequiredException(org.apache.maven.artifact.DependencyResolutionRequiredException)

Example 3 with JarArchiver

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

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

the class AbstractCodegenMojo 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());
    String javaPath = getJavaExecutable().getAbsolutePath();
    cmd.setExecutable(javaPath);
    setJvmForkArgs(javaPath);
    cmd.createArg().setLine(additionalJvmArgs);
    final File file;
    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) 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 5 with JarArchiver

use of org.codehaus.plexus.archiver.jar.JarArchiver in project azure-tools-for-java by Microsoft.

the class JarUtils method buildJarFileToStagingPath.

public static Path buildJarFileToStagingPath(String stagingFolder, Module module) throws IOException {
    final File stagingFolderFile = new File(stagingFolder);
    if (!stagingFolderFile.exists()) {
        stagingFolderFile.mkdirs();
    }
    final String moduleName = module.getName();
    final String path = CompilerPaths.getModuleOutputPath(module, false);
    final Path outputFile = Paths.get(stagingFolder, moduleName + ".jar");
    final JarArchiver jar = new JarArchiver();
    jar.setCompress(true);
    jar.setDestFile(outputFile.toFile());
    jar.addDirectory(new File(path));
    final Manifest manifest = new Manifest();
    manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
    manifest.getMainAttributes().put(new Attributes.Name("Created-By"), "Azure Intellij Plugin");
    try {
        jar.addConfiguredManifest(manifest);
    } catch (ManifestException e) {
        throw new AzureToolkitRuntimeException("Cannot create manifest for function jar.", e);
    }
    jar.createArchive();
    return outputFile;
}
Also used : Path(java.nio.file.Path) Attributes(java.util.jar.Attributes) AzureToolkitRuntimeException(com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException) 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)

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