Search in sources :

Example 46 with CommandLineException

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

the class AbstractJDeprScanMojo method executeJDeprScanCommandLine.

private void executeJDeprScanCommandLine(Commandline cmd, 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 (StringUtils.isNotEmpty(output)) {
            getLog().info(output);
        }
        if (exitCode != 0) {
            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());
        }
    } catch (CommandLineException e) {
        throw new MojoExecutionException("Unable to execute jdeprscan command: " + e.getMessage(), e);
    }
    if (StringUtils.isNotEmpty(err.getOutput()) && getLog().isWarnEnabled()) {
        getLog().warn("JDeprScan 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)

Example 47 with CommandLineException

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

the class AbstractJLinkMojo 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().error(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 jlink 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 48 with CommandLineException

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

the class JavadocUtil method getJavadocVersion.

/**
 * Call the Javadoc tool and parse its output to find its version, i.e.:
 * <pre>
 * javadoc.exe(or .sh) -J-version
 * </pre>
 *
 * @param javadocExe not null file
 * @return the javadoc version as float
 * @throws IOException if javadocExe is null, doesn't exist or is not a file
 * @throws CommandLineException if any
 * @throws IllegalArgumentException if no output was found in the command line
 * @throws PatternSyntaxException if the output contains a syntax error in the regular-expression pattern.
 * @see #extractJavadocVersion(String)
 */
protected static JavadocVersion getJavadocVersion(File javadocExe) throws IOException, CommandLineException, IllegalArgumentException {
    if ((javadocExe == null) || (!javadocExe.exists()) || (!javadocExe.isFile())) {
        throw new IOException("The javadoc executable '" + javadocExe + "' doesn't exist or is not a file. ");
    }
    Commandline cmd = new Commandline();
    cmd.setExecutable(javadocExe.getAbsolutePath());
    cmd.setWorkingDirectory(javadocExe.getParentFile());
    cmd.createArg().setValue("-J-version");
    CommandLineUtils.StringStreamConsumer out = new CommandLineUtils.StringStreamConsumer();
    CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
    int exitCode = CommandLineUtils.executeCommandLine(cmd, out, err);
    if (exitCode != 0) {
        StringBuilder msg = new StringBuilder("Exit code: " + exitCode + " - " + err.getOutput());
        msg.append('\n');
        msg.append("Command line was:" + CommandLineUtils.toString(cmd.getCommandline()));
        throw new CommandLineException(msg.toString());
    }
    if (StringUtils.isNotEmpty(err.getOutput())) {
        return JavadocVersion.parse(extractJavadocVersion(err.getOutput()));
    } else if (StringUtils.isNotEmpty(out.getOutput())) {
        return JavadocVersion.parse(extractJavadocVersion(out.getOutput()));
    }
    throw new IllegalArgumentException("No output found from the command line 'javadoc -J-version'");
}
Also used : Commandline(org.codehaus.plexus.util.cli.Commandline) CommandLineUtils(org.codehaus.plexus.util.cli.CommandLineUtils) IOException(java.io.IOException) CommandLineException(org.codehaus.plexus.util.cli.CommandLineException)

Example 49 with CommandLineException

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

the class Java2WSMojo method processJavaClass.

private void processJavaClass(List<String> args, String cp) throws MojoExecutionException {
    if (!fork) {
        try {
            CommandInterfaceUtils.commandCommonMain();
            JavaToWS j2w = new JavaToWS(args.toArray(new String[0]));
            j2w.run();
        } catch (OutOfMemoryError e) {
            getLog().debug(e);
            StringBuilder msg = new StringBuilder(128);
            msg.append(e.getMessage()).append('\n');
            msg.append("Try to run this goal using the <fork>true</fork> and " + "<additionalJvmArgs>-Xms128m -Xmx128m</additionalJvmArgs> parameters.");
            throw new MojoExecutionException(msg.toString(), e);
        } catch (Throwable e) {
            getLog().debug(e);
            throw new MojoExecutionException(e.getMessage(), e);
        }
    } else {
        getLog().info("Running java2ws 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.addArguments(args.toArray(new String[0]));
        if (classpathAsEnvVar && !StringUtils.isEmpty(cp)) {
            cmd.addEnvironment("CLASSPATH", cp);
        }
        CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
        CommandLineUtils.StringStreamConsumer out = new CommandLineUtils.StringStreamConsumer();
        String cmdLine = CommandLineUtils.toString(cmd.getCommandline());
        int exitCode;
        try {
            exitCode = CommandLineUtils.executeCommandLine(cmd, out, err);
        } catch (CommandLineException e) {
            getLog().debug(e);
            StringBuilder msg = new StringBuilder(e.getMessage());
            if (!(fork && classpathAsEnvVar)) {
                msg.append('\n');
                msg.append("Try to run this goal using <fork>true</fork> and " + "<classpathAsEnvVar>true</classpathAsEnvVar>.");
            }
            msg.append('\n');
            msg.append("Command line was: ").append(cmdLine).append('\n');
            if (classpathAsEnvVar && !StringUtils.isEmpty(cp)) {
                msg.append("   CLASSPATH env: ").append(cp).append('\n');
            }
            msg.append('\n');
            throw new MojoExecutionException(msg.toString(), e);
        }
        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(cmdLine).append('\n');
            if (classpathAsEnvVar && !StringUtils.isEmpty(cp)) {
                msg.append("   CLASSPATH env: ").append(cp).append('\n');
            }
            msg.append('\n');
            throw new MojoExecutionException(msg.toString());
        }
        if (StringUtils.isNotEmpty(err.getOutput()) && err.getOutput().contains("JavaToWS Error")) {
            StringBuilder msg = new StringBuilder();
            msg.append(err.getOutput());
            msg.append('\n');
            msg.append("Command line was: ").append(cmdLine).append('\n');
            if (classpathAsEnvVar && !StringUtils.isEmpty(cp)) {
                msg.append("   CLASSPATH env: ").append(cp).append('\n');
            }
            msg.append('\n');
            throw new MojoExecutionException(msg.toString());
        }
    }
    // with the enclosing project
    if (attachWsdl && outputFile != null) {
        File wsdlFile = new File(outputFile);
        if (wsdlFile.exists()) {
            boolean hasWsdlAttached = false;
            for (Artifact a : project.getAttachedArtifacts()) {
                if ("wsdl".equals(a.getType()) && classifier != null && classifier.equals(a.getClassifier())) {
                    hasWsdlAttached = true;
                }
            }
            if (!hasWsdlAttached) {
                if (classifier != null) {
                    projectHelper.attachArtifact(project, wsdlFile.getName(), classifier, wsdlFile);
                } else {
                    projectHelper.attachArtifact(project, wsdlFile.getName(), wsdlFile);
                }
            }
        }
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Commandline(org.codehaus.plexus.util.cli.Commandline) IOException(java.io.IOException) CommandLineException(org.codehaus.plexus.util.cli.CommandLineException) Artifact(org.apache.maven.artifact.Artifact) CommandLineUtils(org.codehaus.plexus.util.cli.CommandLineUtils) JavaToWS(org.apache.cxf.tools.java2ws.JavaToWS) File(java.io.File)

Example 50 with CommandLineException

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

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