Permalink
Browse files

experiment using commons-exec to avoid "Broken Pipe" error under Windows

  • Loading branch information...
1 parent 84b0b7b commit 583f7bba882fd37eccefecce9e1f1894e8ac8695 @davidB committed Sep 9, 2009
View
@@ -1,3 +1,3 @@
!!org.twdata.maven.yamlpom.SyncFile
-xml: {md5: 0AA97AA1760142B233C7A54C4DC19C78, syncBy: dwayne, timestamp: 1250192330000}
-yaml: {md5: AD76597DC4FE35AF12B47F9AA9EEB863, syncBy: dwayne, timestamp: 1250192330000}
+xml: {md5: 0A3D4478043E6109EBF09BCF64567077, syncBy: dwayne, timestamp: 1252510464000}
+yaml: {md5: D9E313DFD14D36093BD1A240855D735F, syncBy: dwayne, timestamp: 1252510464000}
View
@@ -113,20 +113,28 @@
<artifactId>maven-common-artifact-filters</artifactId>
<version>1.0</version>
</dependency>
+ <dependency>
+ <groupId>org.apache.commons</groupId>
+ <artifactId>commons-exec</artifactId>
+ <version>1.0</version>
+ </dependency>
</dependencies>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
+ <version>2.5</version>
</plugin>
</plugins>
</reporting>
<build>
<plugins>
+<!--
<plugin>
<groupId>org.twdata.maven</groupId>
<artifactId>maven-yamlpom-plugin</artifactId>
+ <version>0.4.1</version>
<executions>
<execution>
<goals>
@@ -135,6 +143,7 @@
</execution>
</executions>
</plugin>
+-->
<plugin>
<artifactId>maven-invoker-plugin</artifactId>
<version>1.3</version>
View
@@ -44,13 +44,15 @@ dependencies:
- { groupId: org.apache.maven.shared, artifactId: maven-dependency-analyzer, version: 1.1 }
- { groupId: org.apache.maven.shared, artifactId: maven-dependency-tree, version: 1.2 }
- { groupId: org.apache.maven.shared, artifactId: maven-common-artifact-filters, version: 1.0 }
+ - { groupId: org.apache.commons, artifactId: commons-exec, version: 1.0 }
reporting:
plugins:
- - { groupId: org.apache.maven.plugins, artifactId: maven-plugin-plugin }
+ - { groupId: org.apache.maven.plugins, artifactId: maven-plugin-plugin, version: 2.5 }
build:
plugins:
- groupId: org.twdata.maven
artifactId: maven-yamlpom-plugin
+ version: 0.4.1
executions:
- goals: [ sync ]
- artifactId: maven-invoker-plugin
@@ -105,7 +105,6 @@ protected void doExecute() throws Exception {
jcmd.addJvmArgs("-noverify", "-javaagent:" + javaRebelPath.getCanonicalPath());
}
}
- jcmd.setLogOnly(false);
jcmd.run(displayCmd);
}
@@ -31,7 +31,7 @@
/**
* The class to use when launching a scala program
- *
+ *
* @parameter expression="${launcher}"
*/
protected String launcher;
@@ -101,7 +101,6 @@ protected void doExecute() throws Exception {
if (StringUtils.isNotEmpty(addArgs)) {
jcmd.addArgs(StringUtils.split(addArgs, "|"));
}
- jcmd.setLogOnly(false);
jcmd.run(displayCmd);
} else {
getLog().warn("Not mainClass or valid launcher found/define");
@@ -7,17 +7,23 @@
import org.codehaus.plexus.util.StringUtils;
import org.scala_tools.maven.StreamLogger;
import org.scala_tools.maven.StreamPiper;
+import org.apache.commons.exec.CommandLine;
+import org.apache.commons.exec.DefaultExecutor;
+import org.apache.commons.exec.Executor;
+import org.apache.commons.exec.LogOutputStream;
+import org.apache.commons.exec.PumpStreamHandler;
+
/**
* Abstract "process builder" object for forked java commands.
- *
+ *
* @author J. Suereth
*/
public abstract class AbstractForkedJavaCommand extends AbstractJavaMainCaller {
/**
* Location of java executable.
*/
protected String javaExec;
-
+
public AbstractForkedJavaCommand(AbstractMojo requester,
String mainClassName, String classpath, String[] jvmArgs, String[] args)
throws Exception {
@@ -46,23 +52,30 @@ public void run(boolean displayCmd, boolean throwFailure) throws Exception {
} else if (requester.getLog().isDebugEnabled()) {
requester.getLog().debug("cmd: " + " " + StringUtils.join(cmd, " "));
}
- ProcessBuilder pb = new ProcessBuilder(cmd);
- //pb.directory("myDir");
- if (!logOnly) {
- pb = pb.redirectErrorStream(true);
+// ProcessBuilder pb = new ProcessBuilder(cmd);
+// //pb.directory("myDir");
+// if (!logOnly) {
+// pb = pb.redirectErrorStream(true);
+// }
+// Process p = pb.start();
+// if (logOnly) {
+// new StreamLogger(p.getErrorStream(), requester.getLog(), true).start();
+// new StreamLogger(p.getInputStream(), requester.getLog(), false).start();
+// } else {
+// new StreamPiper(p.getInputStream(), System.out).start();
+// new StreamPiper(System.in, p.getOutputStream()).start();
+// //new ConsolePiper(p).start();
+// }
+// int retVal = p.waitFor();
+ Executor exec = new DefaultExecutor();
+ CommandLine cl = new CommandLine(cmd[0]);
+ for(int i = 1; i < cmd.length; i++) {
+ cl.addArgument(cmd[i]);
}
- Process p = pb.start();
- if (logOnly) {
- new StreamLogger(p.getErrorStream(), requester.getLog(), true).start();
- new StreamLogger(p.getInputStream(), requester.getLog(), false).start();
- } else {
- new StreamPiper(p.getInputStream(), System.out).start();
- new StreamPiper(System.in, p.getOutputStream()).start();
- //new ConsolePiper(p).start();
- }
- int retVal = p.waitFor();
- if (throwFailure && (retVal != 0)) {
- throw new MojoFailureException("command line returned non-zero value:" + retVal);
+ int exitValue = exec.execute(cl);
+
+ if (throwFailure && (exitValue != 0)) {
+ throw new MojoFailureException("command line returned non-zero value:" + exitValue);
}
}
@@ -13,7 +13,6 @@
*/
public abstract class AbstractJavaMainCaller implements JavaMainCaller {
- protected boolean logOnly = false;
protected AbstractMojo requester;
protected List<String> env = new ArrayList<String>();
protected String mainClassName;
@@ -31,15 +30,15 @@ protected AbstractJavaMainCaller(AbstractMojo requester, String mainClassName, S
addJvmArgs(jvmArgs);
addArgs(args);
}
-
+
public void addJvmArgs(String... args) {
if(args != null) {
for(String arg : args) {
this.jvmArgs.add(arg);
}
}
}
-
+
public void addToClasspath(File entry) throws Exception {
if ((entry == null) || !entry.exists()) {
return;
@@ -57,7 +56,7 @@ public void addToClasspath(File entry) throws Exception {
}
}
}
-
+
public void addOption(String key, String value) {
if ((value == null) || (key == null)) {
return;
@@ -91,12 +90,4 @@ public void addEnvVar(String key, String value) {
public void run(boolean displayCmd) throws Exception {
run(displayCmd, true);
}
-
- public void setLogOnly(boolean v) {
- logOnly = v;
- }
- public boolean getLogOnly() {
- return logOnly;
- }
-
}
@@ -3,41 +3,38 @@
import java.io.File;
/**
* This interface is used to create a call on a main method of a java class.
- *
+ *
* The important implementations are JavaCommand and ReflectionJavaCaller
- *
+ *
* @author J. Suereth
*
*/
public interface JavaMainCaller {
- /** Adds an environemnt variable */
- public abstract void addEnvVar(String key, String value);
- /** Adds a JVM arg. Note: This is not available for in-process "forks" */
- public abstract void addJvmArgs(String... args);
- /** Adds arguments for the process */
- public abstract void addArgs(String... args);
- /** Adds option (basically two arguments) */
- public abstract void addOption(String key, String value);
- /** Adds an option (key-file pair). This will pull the absolute path of the file */
- public abstract void addOption(String key, File value);
- /** Adds the key iff the value is true */
- public abstract void addOption(String key, boolean value);
- /** Sets the process to *only* log errors */
- public abstract void setLogOnly(boolean v);
+ /** Adds an environemnt variable */
+ public abstract void addEnvVar(String key, String value);
+ /** Adds a JVM arg. Note: This is not available for in-process "forks" */
+ public abstract void addJvmArgs(String... args);
+ /** Adds arguments for the process */
+ public abstract void addArgs(String... args);
+ /** Adds option (basically two arguments) */
+ public abstract void addOption(String key, String value);
+ /** Adds an option (key-file pair). This will pull the absolute path of the file */
+ public abstract void addOption(String key, File value);
+ /** Adds the key if the value is true */
+ public abstract void addOption(String key, boolean value);
- // TODO: avoid to have several Thread to pipe stream
- // TODO: add support to inject startup command and shutdown command (on :quit)
- public abstract void run(boolean displayCmd) throws Exception;
- /** Runs the JavaMain with all the built up arguments/options */
- public abstract void run(boolean displayCmd, boolean throwFailure)
- throws Exception;
+ // TODO: avoid to have several Thread to pipe stream
+ // TODO: add support to inject startup command and shutdown command (on :quit)
+ public abstract void run(boolean displayCmd) throws Exception;
+ /** Runs the JavaMain with all the built up arguments/options */
+ public abstract void run(boolean displayCmd, boolean throwFailure) throws Exception;
- /**
- * run the command without stream redirection nor waiting for exit
- *
- * @param displayCmd
- * @throws Exception
- */
- public abstract void spawn(boolean displayCmd) throws Exception;
+ /**
+ * run the command without stream redirection nor waiting for exit
+ *
+ * @param displayCmd
+ * @throws Exception
+ */
+ public abstract void spawn(boolean displayCmd) throws Exception;
}

0 comments on commit 583f7bb

Please sign in to comment.