Search in sources :

Example 91 with CommandLineException

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

the class AbstractJModMojo method executeCommand.

protected void executeCommand(Commandline cmd, File outputDirectory) throws MojoExecutionException {
    if (getLog().isDebugEnabled()) {
        // no quoted arguments ???
        getLog().debug(CommandLineUtils.toString(cmd.getCommandline()).replaceAll("'", ""));
    }
    CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
    CommandLineUtils.StringStreamConsumer 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)) {
                // Reconsider to use WARN / ERROR ?
                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)) {
            String[] splitLines = StringUtils.split(output, "\n");
            for (String outputLine : splitLines) {
                getLog().info(outputLine);
            }
        }
    } catch (CommandLineException e) {
        throw new MojoExecutionException("Unable to execute jmod command: " + e.getMessage(), e);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) CommandLineUtils(org.codehaus.plexus.util.cli.CommandLineUtils) CommandLineException(org.codehaus.plexus.util.cli.CommandLineException)

Example 92 with CommandLineException

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

the class GpgSigner method generateSignatureForFile.

/**
 * {@inheritDoc}
 */
@Override
protected void generateSignatureForFile(File file, File signature) throws MojoExecutionException {
    // ----------------------------------------------------------------------------
    // Set up the command line
    // ----------------------------------------------------------------------------
    Commandline cmd = new Commandline();
    if (StringUtils.isNotEmpty(executable)) {
        cmd.setExecutable(executable);
    } else {
        cmd.setExecutable("gpg" + (Os.isFamily(Os.FAMILY_WINDOWS) ? ".exe" : ""));
    }
    if (args != null) {
        for (String arg : args) {
            cmd.createArg().setValue(arg);
        }
    }
    if (homeDir != null) {
        cmd.createArg().setValue("--homedir");
        cmd.createArg().setFile(homeDir);
    }
    if (useAgent) {
        cmd.createArg().setValue("--use-agent");
    } else {
        cmd.createArg().setValue("--no-use-agent");
    }
    InputStream in = null;
    if (null != passphrase) {
        // make --passphrase-fd effective in gpg2
        cmd.createArg().setValue("--batch");
        cmd.createArg().setValue("--passphrase-fd");
        cmd.createArg().setValue("0");
        // Prepare the input stream which will be used to pass the passphrase to the executable
        in = new ByteArrayInputStream(passphrase.getBytes());
    }
    if (null != keyname) {
        cmd.createArg().setValue("--local-user");
        cmd.createArg().setValue(keyname);
    }
    cmd.createArg().setValue("--armor");
    cmd.createArg().setValue("--detach-sign");
    if (!isInteractive) {
        cmd.createArg().setValue("--no-tty");
    }
    if (!defaultKeyring) {
        cmd.createArg().setValue("--no-default-keyring");
    }
    if (StringUtils.isNotEmpty(secretKeyring)) {
        cmd.createArg().setValue("--secret-keyring");
        cmd.createArg().setValue(secretKeyring);
    }
    if (StringUtils.isNotEmpty(publicKeyring)) {
        cmd.createArg().setValue("--keyring");
        cmd.createArg().setValue(publicKeyring);
    }
    if ("once".equalsIgnoreCase(lockMode)) {
        cmd.createArg().setValue("--lock-once");
    } else if ("multiple".equalsIgnoreCase(lockMode)) {
        cmd.createArg().setValue("--lock-multiple");
    } else if ("never".equalsIgnoreCase(lockMode)) {
        cmd.createArg().setValue("--lock-never");
    }
    cmd.createArg().setValue("--output");
    cmd.createArg().setFile(signature);
    cmd.createArg().setFile(file);
    try {
        int exitCode = CommandLineUtils.executeCommandLine(cmd, in, new DefaultConsumer(), new DefaultConsumer());
        if (exitCode != 0) {
            throw new MojoExecutionException("Exit code: " + exitCode);
        }
    } catch (CommandLineException e) {
        throw new MojoExecutionException("Unable to execute gpg command", e);
    }
}
Also used : Commandline(org.codehaus.plexus.util.cli.Commandline) DefaultConsumer(org.codehaus.plexus.util.cli.DefaultConsumer) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CommandLineException(org.codehaus.plexus.util.cli.CommandLineException)

Example 93 with CommandLineException

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

the class AbstractJavadocMojo method executeJavadocCommandLine.

/**
 * Execute the Javadoc command line
 *
 * @param cmd                    not null
 * @param javadocOutputDirectory not null
 * @throws MavenReportException if any errors occur
 */
private void executeJavadocCommandLine(Commandline cmd, File javadocOutputDirectory) throws MavenReportException {
    if (getLog().isDebugEnabled()) {
        // no quoted arguments
        getLog().debug(CommandLineUtils.toString(cmd.getCommandline()).replaceAll("'", ""));
    }
    String cmdLine = null;
    if (debug) {
        cmdLine = CommandLineUtils.toString(cmd.getCommandline()).replaceAll("'", "");
        cmdLine = JavadocUtil.hideProxyPassword(cmdLine, settings);
        writeDebugJavadocScript(cmdLine, javadocOutputDirectory);
    }
    CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
    CommandLineUtils.StringStreamConsumer 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 (cmdLine == null) {
                cmdLine = CommandLineUtils.toString(cmd.getCommandline()).replaceAll("'", "");
                cmdLine = JavadocUtil.hideProxyPassword(cmdLine, settings);
            }
            writeDebugJavadocScript(cmdLine, javadocOutputDirectory);
            if (StringUtils.isNotEmpty(output) && StringUtils.isEmpty(err.getOutput()) && isJavadocVMInitError(output)) {
                throw new MavenReportException(output + '\n' + '\n' + JavadocUtil.ERROR_INIT_VM + '\n' + "Or, try to reduce the Java heap size for the Javadoc goal using " + "-Dminmemory=<size> and -Dmaxmemory=<size>." + '\n' + '\n' + "Command line was: " + cmdLine + '\n' + '\n' + "Refer to the generated Javadoc files in '" + javadocOutputDirectory + "' dir.\n");
            }
            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(cmdLine).append('\n').append('\n');
            msg.append("Refer to the generated Javadoc files in '").append(javadocOutputDirectory).append("' dir.\n");
            throw new MavenReportException(msg.toString());
        }
        if (StringUtils.isNotEmpty(output)) {
            getLog().info(output);
        }
    } catch (CommandLineException e) {
        throw new MavenReportException("Unable to execute javadoc command: " + e.getMessage(), e);
    }
    if (StringUtils.isNotEmpty(err.getOutput()) && getLog().isWarnEnabled()) {
        getLog().warn("Javadoc 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) CommandLineUtils(org.codehaus.plexus.util.cli.CommandLineUtils) CommandLineException(org.codehaus.plexus.util.cli.CommandLineException) MavenReportException(org.apache.maven.reporting.MavenReportException)

Example 94 with CommandLineException

use of org.codehaus.plexus.util.cli.CommandLineException in project cxf by apache.

the class AbstractCodeGeneratorMojo method runForked.

private void runForked(Set<URI> classPath, Class<?> cls, String[] args) throws MojoExecutionException {
    getLog().info("Running wadl2java in fork mode...");
    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);
    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(cls.getName());
        manifest.getMainSection().addConfiguredAttribute(attr);
        jar.addConfiguredManifest(manifest);
        jar.createArchive();
        cmd.createArg().setValue("-jar");
        cmd.createArg().setValue(file.getAbsolutePath());
    } catch (Exception e1) {
        throw new MojoExecutionException("Could not create runtime jar", e1);
    }
    cmd.addArguments(args);
    CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
    CommandLineUtils.StringStreamConsumer out = new CommandLineUtils.StringStreamConsumer();
    int exitCode;
    try {
        exitCode = CommandLineUtils.executeCommandLine(cmd, out, err);
    } catch (CommandLineException e) {
        getLog().debug(e);
        throw new MojoExecutionException(e.getMessage(), e);
    }
    String output = StringUtils.isEmpty(out.getOutput()) ? null : '\n' + out.getOutput().trim();
    String cmdLine = CommandLineUtils.toString(cmd.getCommandline());
    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(cmdLine).append('\n').append('\n');
        throw new MojoExecutionException(msg.toString());
    }
    if (file != null) {
        file.delete();
    }
    if (StringUtils.isNotEmpty(err.getOutput()) && err.getOutput().contains("WADL2Java Error")) {
        StringBuilder msg = new StringBuilder();
        msg.append(err.getOutput());
        msg.append('\n');
        msg.append("Command line was: ").append(cmdLine).append('\n').append('\n');
        throw new MojoExecutionException(msg.toString());
    }
}
Also used : 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) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) CommandLineException(org.codehaus.plexus.util.cli.CommandLineException) CommandLineUtils(org.codehaus.plexus.util.cli.CommandLineUtils) File(java.io.File) JarArchiver(org.codehaus.plexus.archiver.jar.JarArchiver)

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