Search in sources :

Example 11 with StreamConsumer

use of org.codehaus.plexus.util.cli.StreamConsumer in project maven-plugins by apache.

the class ApplyMojo method applyPatches.

private String applyPatches(Map patchesApplied) throws MojoExecutionException {
    final StringWriter outputWriter = new StringWriter();
    StreamConsumer consumer = new StreamConsumer() {

        public void consumeLine(String line) {
            if (getLog().isDebugEnabled()) {
                getLog().debug(line);
            }
            outputWriter.write(line + "\n");
        }
    };
    // used if failFast is false
    List failedPatches = new ArrayList();
    for (Object o : patchesApplied.entrySet()) {
        Entry entry = (Entry) o;
        String patchName = (String) entry.getKey();
        Commandline cli = (Commandline) entry.getValue();
        try {
            getLog().info("Applying patch: " + patchName);
            int result = executeCommandLine(cli, consumer, consumer);
            if (result != 0) {
                if (failFast) {
                    throw new MojoExecutionException("Patch command failed with exit code " + result + " for " + patchName + ". Please see console and debug output for more information.");
                } else {
                    failedPatches.add(patchName);
                }
            }
        } catch (CommandLineException e) {
            throw new MojoExecutionException("Failed to apply patch: " + patchName + ". See debug output for more information.", e);
        }
    }
    if (!failedPatches.isEmpty()) {
        getLog().error("Failed applying one or more patches:");
        for (Object failedPatche : failedPatches) {
            getLog().error("* " + failedPatche);
        }
        throw new MojoExecutionException("Patch command failed for one or more patches." + " Please see console and debug output for more information.");
    }
    return outputWriter.toString();
}
Also used : StreamConsumer(org.codehaus.plexus.util.cli.StreamConsumer) Entry(java.util.Map.Entry) StringWriter(java.io.StringWriter) Commandline(org.codehaus.plexus.util.cli.Commandline) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) CommandLineException(org.codehaus.plexus.util.cli.CommandLineException)

Example 12 with StreamConsumer

use of org.codehaus.plexus.util.cli.StreamConsumer in project jangaroo-tools by CoreMedia.

the class PhantomJsTestRunner method execute.

public boolean execute() throws CommandLineException {
    final Commandline cmd = createCommandLine();
    final ArrayList<String> arguments = new ArrayList<String>();
    arguments.add(testRunner);
    arguments.add(testPageUrl);
    arguments.add(testResultFilename);
    arguments.add(String.valueOf(timeout));
    cmd.addArguments(arguments.toArray(new String[arguments.size()]));
    final StreamConsumer outConsumer = new StreamConsumer() {

        @Override
        public void consumeLine(String line) {
            Matcher matcher = LOG_LEVEL_PATTERN.matcher(line);
            String logLevel;
            String msg;
            if (matcher.matches()) {
                logLevel = matcher.group(1);
                msg = matcher.group(2);
            } else {
                logLevel = "DEBUG";
                msg = line;
            }
            if ("ERROR".equals(logLevel)) {
                log.error(msg);
            } else if ("WARN".equals(logLevel)) {
                log.warn(msg);
            } else if ("INFO".equals(logLevel)) {
                log.info(msg);
            } else {
                log.debug(msg);
            }
        }
    };
    final StreamConsumer errConsumer = new StreamConsumer() {

        @Override
        public void consumeLine(String line) {
            log.warn(line);
        }
    };
    log.info("executing phantomjs cmd: " + cmd.toString());
    for (int tryCount = 0; tryCount <= maxRetriesOnCrashes; ++tryCount) {
        int returnCode = CommandLineUtils.executeCommandLine(cmd, outConsumer, errConsumer, timeout);
        if (returnCode >= 0 && returnCode <= 4) {
            // valid phantomjs-joounit-page-runner return codes!
            return returnCode == 0;
        }
        log.warn(String.format("unexpected result %d from phantomjs run #%d", returnCode, tryCount + 1));
    }
    log.error(String.format("Got %d unexpected results from phantomjs, giving up.", maxRetriesOnCrashes + 1));
    return false;
}
Also used : StreamConsumer(org.codehaus.plexus.util.cli.StreamConsumer) Commandline(org.codehaus.plexus.util.cli.Commandline) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList)

Example 13 with StreamConsumer

use of org.codehaus.plexus.util.cli.StreamConsumer 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 14 with StreamConsumer

use of org.codehaus.plexus.util.cli.StreamConsumer in project maven-scm by apache.

the class StarteamUpdateCommand method deleteLocal.

private void deleteLocal(StarteamScmProviderRepository repo, ScmFileSet fileSet, ScmVersion version) throws ScmException {
    if (fileSet.getFileList().size() != 0) {
        return;
    }
    Commandline cl = createDeleteLocalCommand(repo, fileSet, version);
    StreamConsumer consumer = new DefaultConsumer();
    CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
    int exitCode = StarteamCommandLineUtils.executeCommandline(cl, consumer, stderr, getLogger());
    if (exitCode != 0) {
        throw new ScmException("Error executing delete-local: " + stderr.toString());
    }
}
Also used : StreamConsumer(org.codehaus.plexus.util.cli.StreamConsumer) ScmException(org.apache.maven.scm.ScmException) Commandline(org.codehaus.plexus.util.cli.Commandline) DefaultConsumer(org.codehaus.plexus.util.cli.DefaultConsumer) StarteamCommandLineUtils(org.apache.maven.scm.provider.starteam.command.StarteamCommandLineUtils) CommandLineUtils(org.codehaus.plexus.util.cli.CommandLineUtils)

Example 15 with StreamConsumer

use of org.codehaus.plexus.util.cli.StreamConsumer in project maven-scm by apache.

the class FileConsumerTest method testConsumeRemoved.

@Test
public void testConsumeRemoved() {
    List<File> extractedFiles = new ArrayList<File>();
    StreamConsumer consumer = new FileConsumer(extractedFiles, FileConsumer.DEFUNCT_PATTERN);
    consumer.consumeLine("Recursively removing \"tcktests/src\" .");
    consumer.consumeLine("Removing \"tcktests/src/main/java/Application.java\" .");
    consumer.consumeLine("Removing \"tcktests/src/main/java\" .");
    consumer.consumeLine("Removing \"tcktests/src/main\" .");
    consumer.consumeLine("Removing \"tcktests/src\" .");
    assertThat(extractedFiles.size(), is(4));
    assertThat(extractedFiles, hasItem(new File("tcktests/src")));
    assertThat(extractedFiles, hasItem(new File("tcktests/src/main/java/Application.java")));
}
Also used : StreamConsumer(org.codehaus.plexus.util.cli.StreamConsumer) ArrayList(java.util.ArrayList) File(java.io.File) Test(org.junit.Test)

Aggregations

StreamConsumer (org.codehaus.plexus.util.cli.StreamConsumer)17 File (java.io.File)10 Commandline (org.codehaus.plexus.util.cli.Commandline)9 ArrayList (java.util.ArrayList)7 CommandLineException (org.codehaus.plexus.util.cli.CommandLineException)5 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)4 Test (org.junit.Test)4 DebugLoggerConsumer (org.apache.maven.scm.provider.jazz.command.consumer.DebugLoggerConsumer)3 ErrorConsumer (org.apache.maven.scm.provider.jazz.command.consumer.ErrorConsumer)3 JazzScmProviderRepository (org.apache.maven.scm.provider.jazz.repository.JazzScmProviderRepository)3 IOException (java.io.IOException)2 URI (java.net.URI)2 AbstractArtifactResolutionException (org.apache.maven.artifact.resolver.AbstractArtifactResolutionException)2 JazzScmCommand (org.apache.maven.scm.provider.jazz.command.JazzScmCommand)2 JarArchiver (org.codehaus.plexus.archiver.jar.JarArchiver)2 Manifest (org.codehaus.plexus.archiver.jar.Manifest)2 Attribute (org.codehaus.plexus.archiver.jar.Manifest.Attribute)2 DefaultConsumer (org.codehaus.plexus.util.cli.DefaultConsumer)2 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1