Search in sources :

Example 1 with StreamConsumer

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

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

the class BootstrapMojo method runGoals.

/**
 * @param relativePathProjectDirectory the project directory's path relative to the checkout
 *                                     directory; or "" if they are the same
 * @throws MojoExecutionException if any
 */
private void runGoals(String relativePathProjectDirectory) throws MojoExecutionException {
    Commandline cl = new Commandline();
    try {
        cl.addSystemEnvironment();
    } catch (Exception e) {
        throw new MojoExecutionException("Can't add system environment variables to mvn command line.", e);
    }
    cl.addEnvironment("MAVEN_TERMINATE_CMD", "on");
    if (this.mavenHome == null) {
        // none windows only
        cl.setExecutable("mvn");
    } else {
        String mvnPath = this.mavenHome.getAbsolutePath() + "/bin/mvn";
        if (Os.isFamily("windows")) {
            String winMvnPath = mvnPath + ".cmd";
            if (!new File(winMvnPath).exists()) {
                winMvnPath = mvnPath + ".bat";
            }
            mvnPath = winMvnPath;
        }
        cl.setExecutable(mvnPath);
    }
    cl.setWorkingDirectory(determineWorkingDirectoryPath(// 
    this.getCheckoutDirectory(), relativePathProjectDirectory, goalsDirectory));
    if (this.goals != null) {
        String[] tokens = StringUtils.split(this.goals, ", ");
        for (int i = 0; i < tokens.length; ++i) {
            cl.createArg().setValue(tokens[i]);
        }
    }
    if (!StringUtils.isEmpty(this.profiles)) {
        cl.createArg().setValue("-P" + this.profiles);
    }
    StreamConsumer consumer = new DefaultConsumer();
    try {
        int result = CommandLineUtils.executeCommandLine(cl, consumer, consumer);
        if (result != 0) {
            throw new MojoExecutionException("Result of mvn execution is: \'" + result + "\'. Release failed.");
        }
    } catch (CommandLineException e) {
        throw new MojoExecutionException("Can't run goal " + goals, e);
    }
}
Also used : StreamConsumer(org.codehaus.plexus.util.cli.StreamConsumer) Commandline(org.codehaus.plexus.util.cli.Commandline) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) DefaultConsumer(org.codehaus.plexus.util.cli.DefaultConsumer) File(java.io.File) CommandLineException(org.codehaus.plexus.util.cli.CommandLineException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) CommandLineException(org.codehaus.plexus.util.cli.CommandLineException)

Example 3 with StreamConsumer

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

the class FileConsumerTest method testConsumePromoted.

@Test
public void testConsumePromoted() {
    List<File> extractedFiles = new ArrayList<File>();
    StreamConsumer consumer = new FileConsumer(extractedFiles, FileConsumer.PROMOTE_PATTERN);
    consumer.consumeLine("Promoted element /./src/main/java/Application.java");
    consumer.consumeLine("Promoted element \\.\\src\\main\\java\\Windows.java");
    assertThat(extractedFiles.size(), is(2));
    assertThat(extractedFiles, hasItem(new File("src/main/java/Application.java")));
    assertThat(extractedFiles, hasItem(new File("src\\main\\java\\Windows.java")));
}
Also used : StreamConsumer(org.codehaus.plexus.util.cli.StreamConsumer) ArrayList(java.util.ArrayList) File(java.io.File) Test(org.junit.Test)

Example 4 with StreamConsumer

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

the class InfoConsumerTest method consume.

private AccuRevInfo consume(String resource) throws IOException {
    AccuRevInfo info = new AccuRevInfo(new File("/my/project/dir"));
    StreamConsumer consumer = new InfoConsumer(info);
    BufferedReader reader = new BufferedReader(new InputStreamReader(this.getClass().getResourceAsStream(resource)));
    String line = reader.readLine();
    while (line != null) {
        consumer.consumeLine(line);
        line = reader.readLine();
    }
    return info;
}
Also used : StreamConsumer(org.codehaus.plexus.util.cli.StreamConsumer) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) File(java.io.File) AccuRevInfo(org.apache.maven.scm.provider.accurev.AccuRevInfo)

Example 5 with StreamConsumer

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

the class JazzTagCommand method executeTagCommand.

/**
 * {@inheritDoc}
 */
protected ScmResult executeTagCommand(ScmProviderRepository repo, ScmFileSet fileSet, String tag, ScmTagParameters scmTagParameters) throws ScmException {
    if (getLogger().isDebugEnabled()) {
        getLogger().debug("Executing tag command...");
    }
    JazzScmProviderRepository jazzRepo = (JazzScmProviderRepository) repo;
    getLogger().debug("Creating Snapshot...");
    StreamConsumer tagConsumer = // No need for a dedicated consumer for this
    new DebugLoggerConsumer(getLogger());
    ErrorConsumer errConsumer = new ErrorConsumer(getLogger());
    JazzScmCommand tagCreateSnapshotCmd = createTagCreateSnapshotCommand(jazzRepo, fileSet, tag, scmTagParameters);
    int status = tagCreateSnapshotCmd.execute(tagConsumer, errConsumer);
    if (status != 0) {
        return new TagScmResult(tagCreateSnapshotCmd.getCommandString(), "Error code for Jazz SCM tag (SNAPSHOT) command - " + status, errConsumer.getOutput(), false);
    }
    // ------------------------------------------------------------------
    // We create the workspace based on the tag here, as the scm tool
    // can not currently check directly out from a snapshot (only a workspace).
    getLogger().debug("Creating Workspace from Snapshot...");
    JazzScmCommand tagCreateWorkspaceCmd = createTagCreateWorkspaceCommand(jazzRepo, fileSet, tag);
    errConsumer = new ErrorConsumer(getLogger());
    status = tagCreateWorkspaceCmd.execute(tagConsumer, errConsumer);
    if (status != 0) {
        return new TagScmResult(tagCreateWorkspaceCmd.getCommandString(), "Error code for Jazz SCM tag (WORKSPACE) command - " + status, errConsumer.getOutput(), false);
    }
    if (jazzRepo.isPushChangesAndHaveFlowTargets()) {
        // isPushChanges = true, and we have something to deliver and promote to.
        getLogger().debug("Promoting and delivering...");
        // So we deliver the code to the target stream (or workspace)
        getLogger().debug("Delivering...");
        JazzScmCommand tagDeliverCommand = createTagDeliverCommand(jazzRepo, fileSet, tag);
        errConsumer = new ErrorConsumer(getLogger());
        status = tagDeliverCommand.execute(tagConsumer, errConsumer);
        if (status != 0) {
            return new TagScmResult(tagDeliverCommand.getCommandString(), "Error code for Jazz SCM deliver command - " + status, errConsumer.getOutput(), false);
        }
        // And now we promote the snapshot to the target stream (or workspace)
        getLogger().debug("Promoting snapshot...");
        JazzScmCommand tagSnapshotPromoteCommand = createTagSnapshotPromoteCommand(jazzRepo, fileSet, tag);
        errConsumer = new ErrorConsumer(getLogger());
        status = tagSnapshotPromoteCommand.execute(tagConsumer, errConsumer);
        if (status != 0) {
            return new TagScmResult(tagSnapshotPromoteCommand.getCommandString(), "Error code for Jazz SCM snapshot promote command - " + status, errConsumer.getOutput(), false);
        }
    }
    // We don't have a JazzTagConsumer so just build up all the files...
    List<ScmFile> taggedFiles = new ArrayList<ScmFile>(fileSet.getFileList().size());
    for (File f : fileSet.getFileList()) {
        taggedFiles.add(new ScmFile(f.getPath(), ScmFileStatus.TAGGED));
    }
    // So we return tagSnapshotCmd and not tagWorkspaceCmd.
    return new TagScmResult(tagCreateSnapshotCmd.getCommandString(), taggedFiles);
}
Also used : StreamConsumer(org.codehaus.plexus.util.cli.StreamConsumer) ErrorConsumer(org.apache.maven.scm.provider.jazz.command.consumer.ErrorConsumer) ArrayList(java.util.ArrayList) JazzScmCommand(org.apache.maven.scm.provider.jazz.command.JazzScmCommand) TagScmResult(org.apache.maven.scm.command.tag.TagScmResult) DebugLoggerConsumer(org.apache.maven.scm.provider.jazz.command.consumer.DebugLoggerConsumer) ScmFile(org.apache.maven.scm.ScmFile) File(java.io.File) JazzScmProviderRepository(org.apache.maven.scm.provider.jazz.repository.JazzScmProviderRepository) ScmFile(org.apache.maven.scm.ScmFile)

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