Class CommandLineUtils
- java.lang.Object
-
- org.apache.maven.shared.utils.cli.CommandLineUtils
-
public abstract class CommandLineUtils extends java.lang.Object
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classCommandLineUtils.StringStreamConsumerAStreamConsumerproviding consumed lines as aString.
-
Field Summary
Fields Modifier and Type Field Description private static longMILLIS_PER_SECONDNumber of milliseconds per second.private static longNANOS_PER_SECONDNumber of nanoseconds per second.
-
Constructor Summary
Constructors Constructor Description CommandLineUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description (package private) static java.util.PropertiesensureCaseSensitivity(java.util.Map<java.lang.String,java.lang.String> envs, boolean preserveKeyCase)static intexecuteCommandLine(Commandline cl, java.io.InputStream systemIn, StreamConsumer systemOut, StreamConsumer systemErr)static intexecuteCommandLine(Commandline cl, java.io.InputStream systemIn, StreamConsumer systemOut, StreamConsumer systemErr, int timeoutInSeconds)static intexecuteCommandLine(Commandline cl, java.io.InputStream systemIn, StreamConsumer systemOut, StreamConsumer systemErr, int timeoutInSeconds, java.lang.Runnable runAfterProcessTermination)static intexecuteCommandLine(Commandline cl, java.io.InputStream systemIn, StreamConsumer systemOut, StreamConsumer systemErr, int timeoutInSeconds, java.lang.Runnable runAfterProcessTermination, java.nio.charset.Charset streamCharset)static intexecuteCommandLine(Commandline cl, StreamConsumer systemOut, StreamConsumer systemErr)static intexecuteCommandLine(Commandline cl, StreamConsumer systemOut, StreamConsumer systemErr, int timeoutInSeconds)static CommandLineCallableexecuteCommandLineAsCallable(Commandline cl, java.io.InputStream systemIn, StreamConsumer systemOut, StreamConsumer systemErr, int timeoutInSeconds, java.lang.Runnable runAfterProcessTermination)Immediately forks a process, returns a callable that will block until process is complete.static CommandLineCallableexecuteCommandLineAsCallable(Commandline cl, java.io.InputStream systemIn, StreamConsumer systemOut, StreamConsumer systemErr, int timeoutInSeconds, java.lang.Runnable runAfterProcessTermination, java.nio.charset.Charset streamCharset)Immediately forks a process, returns a callable that will block until process is complete.static java.util.PropertiesgetSystemEnvVars()Deprecated.use System#getenv()static java.util.PropertiesgetSystemEnvVars(boolean caseSensitive)Deprecated.use System#getenv()private static booleanisAlive(java.lang.Process p)static java.lang.StringtoString(java.lang.String... line)static java.lang.String[]translateCommandline(java.lang.String toProcess)
-
-
-
Field Detail
-
MILLIS_PER_SECOND
private static final long MILLIS_PER_SECOND
Number of milliseconds per second.- See Also:
- Constant Field Values
-
NANOS_PER_SECOND
private static final long NANOS_PER_SECOND
Number of nanoseconds per second.- See Also:
- Constant Field Values
-
-
Method Detail
-
executeCommandLine
public static int executeCommandLine(@Nonnull Commandline cl, StreamConsumer systemOut, StreamConsumer systemErr) throws CommandLineException- Parameters:
cl- The command lineCommandlinesystemOut-StreamConsumersystemErr-StreamConsumer- Returns:
- return code.
- Throws:
CommandLineException- in case of a problem.
-
executeCommandLine
public static int executeCommandLine(@Nonnull Commandline cl, StreamConsumer systemOut, StreamConsumer systemErr, int timeoutInSeconds) throws CommandLineException- Parameters:
cl- The command lineCommandlinesystemOut-StreamConsumersystemErr-StreamConsumertimeoutInSeconds- The timeout.- Returns:
- return code.
- Throws:
CommandLineException- in case of a problem.
-
executeCommandLine
public static int executeCommandLine(@Nonnull Commandline cl, java.io.InputStream systemIn, StreamConsumer systemOut, StreamConsumer systemErr) throws CommandLineException- Parameters:
cl- The command lineCommandlinesystemIn-StreamConsumersystemOut-StreamConsumersystemErr-StreamConsumer- Returns:
- return code.
- Throws:
CommandLineException- in case of a problem.
-
executeCommandLine
public static int executeCommandLine(@Nonnull Commandline cl, java.io.InputStream systemIn, StreamConsumer systemOut, StreamConsumer systemErr, int timeoutInSeconds) throws CommandLineException- Parameters:
cl- The command line to executesystemIn- The input to read from, must be thread safesystemOut- A consumer that receives output, must be thread safesystemErr- A consumer that receives system error stream output, must be thread safetimeoutInSeconds- Positive integer to specify timeout, zero and negative integers for no timeout.- Returns:
- A return value, see
Process.exitValue() - Throws:
CommandLineException- or CommandLineTimeOutException if time out occurs
-
executeCommandLine
public static int executeCommandLine(@Nonnull Commandline cl, java.io.InputStream systemIn, StreamConsumer systemOut, StreamConsumer systemErr, int timeoutInSeconds, @Nullable java.lang.Runnable runAfterProcessTermination) throws CommandLineException- Parameters:
cl- The command line to executesystemIn- The input to read from, must be thread safesystemOut- A consumer that receives output, must be thread safesystemErr- A consumer that receives system error stream output, must be thread safetimeoutInSeconds- Positive integer to specify timeout, zero and negative integers for no timeout.runAfterProcessTermination- Optional callback to run after the process terminated or the the timeout was exceeded, but before waiting on the stream feeder and pumpers to finish.- Returns:
- A return value, see
Process.exitValue() - Throws:
CommandLineException- or CommandLineTimeOutException if time out occurs
-
executeCommandLine
public static int executeCommandLine(@Nonnull Commandline cl, java.io.InputStream systemIn, StreamConsumer systemOut, StreamConsumer systemErr, int timeoutInSeconds, @Nullable java.lang.Runnable runAfterProcessTermination, @Nullable java.nio.charset.Charset streamCharset) throws CommandLineException- Parameters:
cl- The command line to executesystemIn- The input to read from, must be thread safesystemOut- A consumer that receives output, must be thread safesystemErr- A consumer that receives system error stream output, must be thread safetimeoutInSeconds- Positive integer to specify timeout, zero and negative integers for no timeout.runAfterProcessTermination- Optional callback to run after the process terminated or the the timeout was exceeded, but before waiting on the stream feeder and pumpers to finish.streamCharset- Charset to use for reading streams- Returns:
- A return value, see
Process.exitValue() - Throws:
CommandLineException- or CommandLineTimeOutException if time out occurs
-
executeCommandLineAsCallable
public static CommandLineCallable executeCommandLineAsCallable(@Nonnull Commandline cl, @Nullable java.io.InputStream systemIn, StreamConsumer systemOut, StreamConsumer systemErr, int timeoutInSeconds, @Nullable java.lang.Runnable runAfterProcessTermination) throws CommandLineException
Immediately forks a process, returns a callable that will block until process is complete.- Parameters:
cl- The command line to executesystemIn- The input to read from, must be thread safesystemOut- A consumer that receives output, must be thread safesystemErr- A consumer that receives system error stream output, must be thread safetimeoutInSeconds- Positive integer to specify timeout, zero and negative integers for no timeout.runAfterProcessTermination- Optional callback to run after the process terminated or the the timeout was- Returns:
- A CommandLineCallable that provides the process return value, see
Process.exitValue(). "call" must be called on this to be sure the forked process has terminated, no guarantees is made about any internal state before after the completion of the call statements - Throws:
CommandLineException- or CommandLineTimeOutException if time out occurs
-
executeCommandLineAsCallable
public static CommandLineCallable executeCommandLineAsCallable(@Nonnull Commandline cl, @Nullable java.io.InputStream systemIn, StreamConsumer systemOut, StreamConsumer systemErr, int timeoutInSeconds, @Nullable java.lang.Runnable runAfterProcessTermination, @Nullable java.nio.charset.Charset streamCharset) throws CommandLineException
Immediately forks a process, returns a callable that will block until process is complete.- Parameters:
cl- The command line to executesystemIn- The input to read from, must be thread safesystemOut- A consumer that receives output, must be thread safesystemErr- A consumer that receives system error stream output, must be thread safetimeoutInSeconds- Positive integer to specify timeout, zero and negative integers for no timeout.runAfterProcessTermination- Optional callback to run after the process terminated or the the timeout wasstreamCharset- Charset to use for reading streams- Returns:
- A CommandLineCallable that provides the process return value, see
Process.exitValue(). "call" must be called on this to be sure the forked process has terminated, no guarantees is made about any internal state before after the completion of the call statements - Throws:
CommandLineException- or CommandLineTimeOutException if time out occurs
-
getSystemEnvVars
@Deprecated public static java.util.Properties getSystemEnvVars()
Deprecated.use System#getenv()Gets the shell environment variables for this process. Note that the returned mapping from variable names to values will always be case-sensitive regardless of the platform, i.e.getSystemEnvVars().get("path")andgetSystemEnvVars().get("PATH")will in general return different values. However, on platforms with case-insensitive environment variables like Windows, all variable names will be normalized to upper case.- Returns:
- The shell environment variables, can be empty but never
null.
-
getSystemEnvVars
@Deprecated public static java.util.Properties getSystemEnvVars(boolean caseSensitive)
Deprecated.use System#getenv()Return the shell environment variables. IfcaseSensitive == true, then envar keys will all be upper-case.- Parameters:
caseSensitive- Whether environment variable keys should be treated case-sensitively.- Returns:
- Properties object of (possibly modified) envar keys mapped to their values.
-
isAlive
private static boolean isAlive(java.lang.Process p)
-
translateCommandline
public static java.lang.String[] translateCommandline(java.lang.String toProcess) throws CommandLineException- Parameters:
toProcess- The command line to translate.- Returns:
- The array of translated parts.
- Throws:
CommandLineException- in case of unbalanced quotes.
-
toString
public static java.lang.String toString(java.lang.String... line)
- Parameters:
line- the lines- Returns:
- the concatenated lines, quoted and escaped, separated by spaces
-
ensureCaseSensitivity
static java.util.Properties ensureCaseSensitivity(java.util.Map<java.lang.String,java.lang.String> envs, boolean preserveKeyCase)
-
-