Search in sources :

Example 1 with Commandline

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

the class JLinkMojo method createJLinkCommandLine.

Commandline createJLinkCommandLine() throws IOException {
    File file = new File(outputDirectory.getParentFile(), "jlinkArgs");
    if (!getLog().isDebugEnabled()) {
        file.deleteOnExit();
    }
    file.getParentFile().mkdirs();
    file.createNewFile();
    PrintStream argsFile = new PrintStream(file);
    if (stripDebug) {
        argsFile.println("--strip-debug");
    }
    if (compression != null) {
        argsFile.println("--compression");
        argsFile.println(compression);
    }
    if (modulePaths != null) {
        argsFile.println("--module-path");
        argsFile.append('"').append(getColonSeparateList(modulePaths).replace("\\", "\\\\")).println('"');
    }
    if (limitModules != null && !limitModules.isEmpty()) {
        argsFile.println("--limit-modules");
        StringBuilder sb = getCommaSeparatedList(limitModules);
        argsFile.println(sb.toString());
    }
    if (addModules != null && !addModules.isEmpty()) {
        argsFile.println("--add-modules");
        StringBuilder sb = getCommaSeparatedList(addModules);
        argsFile.println(sb.toString());
    }
    if (outputDirectory != null) {
        argsFile.println("--output");
        argsFile.println(outputDirectory);
    }
    argsFile.close();
    Commandline cmd = new Commandline();
    cmd.createArg().setValue('@' + file.getAbsolutePath());
    return cmd;
}
Also used : PrintStream(java.io.PrintStream) Commandline(org.codehaus.plexus.util.cli.Commandline) File(java.io.File)

Example 2 with Commandline

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

the class Java2WSMojo method processJavaClass.

private void processJavaClass(List<String> args) throws MojoExecutionException {
    if (!fork) {
        try {
            CommandInterfaceUtils.commandCommonMain();
            JavaToWS j2w = new JavaToWS(args.toArray(new String[args.size()]));
            j2w.run();
        } catch (OutOfMemoryError e) {
            getLog().debug(e);
            StringBuilder msg = new StringBuilder();
            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[args.size()]));
        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 (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').append('\n');
            throw new MojoExecutionException(msg.toString());
        }
    }
    // with the enclosing project
    if (attachWsdl && outputFile != null) {
        File wsdlFile = new File(outputFile);
        if (wsdlFile.exists()) {
            if (classifier != null) {
                projectHelper.attachArtifact(project, wsdlFile.getName(), classifier, wsdlFile);
            } else {
                projectHelper.attachArtifact(project, wsdlFile.getName(), wsdlFile);
            }
            boolean hasWsdlAttached = false;
            for (Artifact a : project.getAttachedArtifacts()) {
                if ("wsdl".equals(a.getType())) {
                    hasWsdlAttached = true;
                }
            }
            if (!hasWsdlAttached) {
                if (classifier != null) {
                    projectHelper.attachArtifact(project, "wsdl", classifier, wsdlFile);
                } else {
                    projectHelper.attachArtifact(project, "wsdl", 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 3 with Commandline

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

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

the class BazaarConfig method getPythonVersion.

public static VersionConsumer getPythonVersion(File workingDir) throws ScmException {
    String[] versionCmd = new String[] { PYTHON_VERSION };
    VersionConsumer consumer = new VersionConsumer(PYTHON_VERSION_TAG);
    Commandline cmd = buildPythonCmd(workingDir, versionCmd);
    // Execute command
    BazaarUtils.executeCmd(consumer, cmd);
    // Return result
    return consumer;
}
Also used : Commandline(org.codehaus.plexus.util.cli.Commandline)

Example 5 with Commandline

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

the class BazaarUtils method buildCmd.

static Commandline buildCmd(File workingDir, String[] cmdAndArgs) throws ScmException {
    Commandline cmd = new Commandline();
    cmd.setExecutable(BazaarConstants.EXEC);
    cmd.setWorkingDirectory(workingDir.getAbsolutePath());
    cmd.addArguments(cmdAndArgs);
    if (!workingDir.exists()) {
        boolean success = workingDir.mkdirs();
        if (!success) {
            String msg = "Working directory did not exist" + " and it couldn't be created: " + workingDir;
            throw new ScmException(msg);
        }
    }
    return cmd;
}
Also used : ScmException(org.apache.maven.scm.ScmException) Commandline(org.codehaus.plexus.util.cli.Commandline)

Aggregations

Commandline (org.codehaus.plexus.util.cli.Commandline)446 File (java.io.File)133 CommandLineUtils (org.codehaus.plexus.util.cli.CommandLineUtils)117 ScmException (org.apache.maven.scm.ScmException)84 CommandLineException (org.codehaus.plexus.util.cli.CommandLineException)70 ScmRepository (org.apache.maven.scm.repository.ScmRepository)51 IOException (java.io.IOException)48 ScmFileSet (org.apache.maven.scm.ScmFileSet)34 Test (org.junit.Test)28 StringStreamConsumer (org.codehaus.plexus.util.cli.CommandLineUtils.StringStreamConsumer)26 ArrayList (java.util.ArrayList)22 StarteamScmProviderRepository (org.apache.maven.scm.provider.starteam.repository.StarteamScmProviderRepository)22 PerforceScmProviderRepository (org.apache.maven.scm.provider.perforce.repository.PerforceScmProviderRepository)19 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)18 SvnCommandLineUtils (org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils)17 GitCommandLineUtils (org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils)16 GitScmProviderRepository (org.apache.maven.scm.provider.git.repository.GitScmProviderRepository)16 SvnScmProviderRepository (org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository)16 ScmFile (org.apache.maven.scm.ScmFile)13 CvsScmProviderRepository (org.apache.maven.scm.provider.cvslib.repository.CvsScmProviderRepository)13