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());
}
}
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);
}
}
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")));
}
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;
}
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);
}
Aggregations