Search in sources :

Example 41 with CommandLineException

use of org.codehaus.plexus.util.cli.CommandLineException in project tycho by eclipse.

the class JDTCompiler method compileOutOfProcess.

/**
 * Compile the java sources in a external process, calling an external executable, like javac.
 *
 * @param workingDirectory
 *            base directory where the process will be launched
 * @param executable
 *            name of the executable to launch
 * @param args
 *            arguments for the executable launched
 * @return CompilerResult with the errors and warnings encountered.
 * @throws CompilerException
 */
CompilerResult compileOutOfProcess(File workingDirectory, String executable, String[] args) throws CompilerException {
    if (true) /* fork is not supported */
    {
        throw new UnsupportedOperationException("compileoutOfProcess not supported");
    }
    Commandline cli = new Commandline();
    cli.setWorkingDirectory(workingDirectory.getAbsolutePath());
    cli.setExecutable(executable);
    cli.addArguments(args);
    CommandLineUtils.StringStreamConsumer out = new CommandLineUtils.StringStreamConsumer();
    CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
    int returnCode;
    List<CompilerMessage> messages;
    try {
        returnCode = CommandLineUtils.executeCommandLine(cli, out, err);
        messages = parseModernStream(new BufferedReader(new StringReader(err.getOutput())));
    } catch (CommandLineException e) {
        throw new CompilerException("Error while executing the external compiler.", e);
    } catch (IOException e) {
        throw new CompilerException("Error while executing the external compiler.", e);
    }
    if (returnCode != 0 && messages.isEmpty()) {
        // TODO: exception?
        messages.add(new CompilerMessage("Failure executing javac,  but could not parse the error:" + EOL + err.getOutput(), Kind.ERROR));
    }
    return new CompilerResult(returnCode == 0, messages);
}
Also used : CompilerMessage(org.codehaus.plexus.compiler.CompilerMessage) Commandline(org.codehaus.plexus.util.cli.Commandline) CompilerResult(org.codehaus.plexus.compiler.CompilerResult) IOException(java.io.IOException) CommandLineException(org.codehaus.plexus.util.cli.CommandLineException) CommandLineUtils(org.codehaus.plexus.util.cli.CommandLineUtils) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) CompilerException(org.codehaus.plexus.compiler.CompilerException)

Example 42 with CommandLineException

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

the class StarteamChangeLogCommand method executeChangeLogCommand.

// ----------------------------------------------------------------------
// AbstractChangeLogCommand Implementation
// ----------------------------------------------------------------------
/**
 * {@inheritDoc}
 */
protected ChangeLogScmResult executeChangeLogCommand(ScmProviderRepository repo, ScmFileSet fileSet, Date startDate, Date endDate, ScmBranch branch, String datePattern) throws ScmException {
    if ((branch != null || StringUtils.isNotEmpty((branch == null) ? null : branch.getName())) && (getLogger().isWarnEnabled())) {
        getLogger().warn("This provider doesn't support changelog with on a given branch.");
    }
    StarteamScmProviderRepository repository = (StarteamScmProviderRepository) repo;
    // TODO: revision
    Commandline cl = createCommandLine(repository, fileSet, startDate);
    StarteamChangeLogConsumer consumer = new StarteamChangeLogConsumer(fileSet.getBasedir(), getLogger(), startDate, endDate, datePattern);
    CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
    int exitCode;
    try {
        exitCode = CommandLineUtils.executeCommandLine(cl, consumer, stderr);
    } catch (CommandLineException ex) {
        throw new ScmException("Error while executing command.", ex);
    }
    if (exitCode != 0) {
        return new ChangeLogScmResult(cl.toString(), "The 'stcmd' command failed.", stderr.getOutput(), false);
    }
    return new ChangeLogScmResult(cl.toString(), new ChangeLogSet(consumer.getModifications(), startDate, endDate));
}
Also used : ScmException(org.apache.maven.scm.ScmException) ChangeLogSet(org.apache.maven.scm.command.changelog.ChangeLogSet) Commandline(org.codehaus.plexus.util.cli.Commandline) StarteamScmProviderRepository(org.apache.maven.scm.provider.starteam.repository.StarteamScmProviderRepository) StarteamCommandLineUtils(org.apache.maven.scm.provider.starteam.command.StarteamCommandLineUtils) CommandLineUtils(org.codehaus.plexus.util.cli.CommandLineUtils) ChangeLogScmResult(org.apache.maven.scm.command.changelog.ChangeLogScmResult) CommandLineException(org.codehaus.plexus.util.cli.CommandLineException)

Example 43 with CommandLineException

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

the class AbstractInvokerMojo method resolveExternalJreVersion.

private CharSequence resolveExternalJreVersion() {
    Artifact pluginArtifact = mojoExecution.getMojoDescriptor().getPluginDescriptor().getPluginArtifact();
    pluginArtifact.getFile();
    Commandline commandLine = new Commandline();
    commandLine.setExecutable(new File(javaHome, "bin/java").getAbsolutePath());
    commandLine.createArg().setValue("-cp");
    commandLine.createArg().setFile(pluginArtifact.getFile());
    commandLine.createArg().setValue(SystemPropertyPrinter.class.getName());
    commandLine.createArg().setValue("java.version");
    final StringBuilder actualJreVersion = new StringBuilder();
    StreamConsumer consumer = new StreamConsumer() {

        public void consumeLine(String line) {
            actualJreVersion.append(line);
        }
    };
    try {
        CommandLineUtils.executeCommandLine(commandLine, consumer, null);
    } catch (CommandLineException e) {
        getLog().warn(e.getMessage());
    }
    return actualJreVersion;
}
Also used : StreamConsumer(org.codehaus.plexus.util.cli.StreamConsumer) Commandline(org.codehaus.plexus.util.cli.Commandline) File(java.io.File) Artifact(org.apache.maven.artifact.Artifact) CommandLineException(org.codehaus.plexus.util.cli.CommandLineException)

Example 44 with CommandLineException

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

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

the class AbstractJDepsMojo method executeJDepsCommandLine.

private void executeJDepsCommandLine(Commandline cmd, File jOutputDirectory, CommandLineUtils.StringStreamConsumer consumer) throws MojoExecutionException {
    if (getLog().isDebugEnabled()) {
        // no quoted arguments
        getLog().debug("Executing: " + CommandLineUtils.toString(cmd.getCommandline()).replaceAll("'", ""));
    }
    CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
    CommandLineUtils.StringStreamConsumer out;
    if (consumer != null) {
        out = consumer;
    } else {
        out = new CommandLineUtils.StringStreamConsumer();
    }
    try {
        int exitCode = CommandLineUtils.executeCommandLine(cmd, out, err);
        String output = (StringUtils.isEmpty(out.getOutput()) ? null : '\n' + out.getOutput().trim());
        if (exitCode != 0) {
            if (StringUtils.isNotEmpty(output)) {
                getLog().info(output);
            }
            StringBuilder msg = new StringBuilder("\nExit code: ");
            msg.append(exitCode);
            if (StringUtils.isNotEmpty(err.getOutput())) {
                msg.append(" - ").append(err.getOutput());
            }
            msg.append('\n');
            msg.append("Command line was: ").append(cmd).append('\n').append('\n');
            throw new MojoExecutionException(msg.toString());
        }
        if (StringUtils.isNotEmpty(output)) {
            getLog().info(output);
        }
    } catch (CommandLineException e) {
        throw new MojoExecutionException("Unable to execute jdeps command: " + e.getMessage(), e);
    }
    if (StringUtils.isNotEmpty(err.getOutput()) && getLog().isWarnEnabled()) {
        getLog().warn("JDeps Warnings");
        StringTokenizer token = new StringTokenizer(err.getOutput(), "\n");
        while (token.hasMoreTokens()) {
            String current = token.nextToken().trim();
            getLog().warn(current);
        }
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) CommandLineUtils(org.codehaus.plexus.util.cli.CommandLineUtils) CommandLineException(org.codehaus.plexus.util.cli.CommandLineException)

Aggregations

CommandLineException (org.codehaus.plexus.util.cli.CommandLineException)94 CommandLineUtils (org.codehaus.plexus.util.cli.CommandLineUtils)76 Commandline (org.codehaus.plexus.util.cli.Commandline)67 ScmException (org.apache.maven.scm.ScmException)56 IOException (java.io.IOException)20 SvnCommandLineUtils (org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils)18 File (java.io.File)14 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)13 BufferedReader (java.io.BufferedReader)6 BlameScmResult (org.apache.maven.scm.command.blame.BlameScmResult)6 StatusScmResult (org.apache.maven.scm.command.status.StatusScmResult)6 SvnScmProviderRepository (org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository)6 InputStreamReader (java.io.InputStreamReader)5 ChangeLogScmResult (org.apache.maven.scm.command.changelog.ChangeLogScmResult)5 ChangeLogSet (org.apache.maven.scm.command.changelog.ChangeLogSet)5 CheckOutScmResult (org.apache.maven.scm.command.checkout.CheckOutScmResult)5 StreamConsumer (org.codehaus.plexus.util.cli.StreamConsumer)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 StringTokenizer (java.util.StringTokenizer)4 CheckInScmResult (org.apache.maven.scm.command.checkin.CheckInScmResult)4