public class Commandline
extends java.lang.Object
implements java.lang.Cloneable
Commandline objects help handling command lines specifying processes to execute.
The class can be used to define a command line as nested elements or as a helper to define a command line by an application.
<someelement>
<acommandline executable="/executable/to/run">
<argument value="argument 1" />
<argument line="argument_1 argument_2 argument_3" />
<argument value="argument 4" />
</acommandline>
</someelement>
The element someelement
must provide a method createAcommandline
which returns an instance
of this class.
Modifier and Type | Class and Description |
---|---|
static class |
Commandline.Argument |
class |
Commandline.Marker
Class to keep track of the position of an Argument.
|
Modifier and Type | Field and Description |
---|---|
protected java.util.Vector<Arg> |
arguments |
protected java.util.Map<java.lang.String,java.lang.String> |
envVars |
protected java.lang.String |
executable
Deprecated.
Use
setExecutable(String) instead. |
protected static java.lang.String |
OS_NAME
Deprecated.
Use
Os class instead. |
private long |
pid |
private Shell |
shell |
protected static java.lang.String |
WINDOWS
Deprecated.
Use
Os class instead. |
private java.io.File |
workingDir
Deprecated.
Use
setWorkingDirectory(File) or setWorkingDirectory(String)
instead. |
Constructor and Description |
---|
Commandline()
Create a new command line object.
|
Commandline(Shell shell)
Create a new command line object.
|
Commandline(java.lang.String toProcess)
Create a new command line object, given a command following POSIX sh quoting rules
|
Commandline(java.lang.String toProcess,
Shell shell)
Create a new command line object.
|
Modifier and Type | Method and Description |
---|---|
void |
addArg(Arg argument)
Adds an argument object to our list of args.
|
void |
addArg(Arg argument,
boolean insertAtStart)
Adds an argument object to our list of args.
|
void |
addArguments(java.lang.String[] line) |
void |
addEnvironment(java.lang.String name,
java.lang.String value)
Add an environment variable
|
void |
addSystemEnvironment()
Add system environment variables
|
void |
clear()
Clear out the whole command line.
|
void |
clearArgs()
Clear out the arguments but leave the executable in place for another operation.
|
java.lang.Object |
clone() |
Arg |
createArg()
Creates an argument object.
|
Arg |
createArg(boolean insertAtStart)
Creates an argument object and adds it to our list of args.
|
Commandline.Argument |
createArgument()
Deprecated.
Use
createArg() instead |
Commandline.Argument |
createArgument(boolean insertAtStart)
Deprecated.
Use
createArg(boolean) instead |
Commandline.Marker |
createMarker()
Return a marker.
|
java.lang.Process |
execute()
Executes the command.
|
java.lang.String[] |
getArguments()
Returns all arguments defined by
addLine , addValue or the argument object. |
java.lang.String[] |
getCommandline()
Returns the executable and all defined arguments.
For Windows Family, getShellCommandline() is returned |
java.lang.String[] |
getEnvironmentVariables()
Return the list of environment variables
|
java.lang.String |
getExecutable()
Return an executable name, quoted for shell use.
|
java.lang.String |
getLiteralExecutable() |
long |
getPid() |
java.lang.String[] |
getRawCommandline()
Returns the executable and all defined arguments.
|
Shell |
getShell()
Get the shell to be used in this command line.
|
java.lang.String[] |
getShellCommandline()
Returns the shell, executable and all defined arguments.
|
java.util.Properties |
getSystemEnvVars() |
java.io.File |
getWorkingDirectory() |
static java.lang.String |
quoteArgument(java.lang.String argument)
Deprecated.
Use
CommandLineUtils.quote(String) instead. |
private void |
setDefaultShell()
Sets the shell or command-line interpreter for the detected operating system, and the shell arguments.
|
void |
setExecutable(java.lang.String executable)
Sets the executable to run.
|
void |
setPid(long pid) |
void |
setShell(Shell shell)
Allows to set the shell to be used in this command line.
|
void |
setWorkingDirectory(java.io.File workingDirectory)
Sets execution directory.
|
void |
setWorkingDirectory(java.lang.String path)
Sets execution directory.
|
int |
size() |
java.lang.String |
toString() |
static java.lang.String |
toString(java.lang.String[] line)
Deprecated.
Use
CommandLineUtils.toString(String[]) instead. |
static java.lang.String[] |
translateCommandline(java.lang.String toProcess)
Deprecated.
Use
CommandLineUtils.translateCommandline(String) instead. |
private void |
verifyShellState()
Deprecated.
Remove once backward compat with plexus-utils <= 1.4 is no longer a consideration
|
protected static final java.lang.String OS_NAME
Os
class instead.protected static final java.lang.String WINDOWS
Os
class instead.protected java.util.Vector<Arg> arguments
protected java.util.Map<java.lang.String,java.lang.String> envVars
private long pid
private Shell shell
protected java.lang.String executable
setExecutable(String)
instead.private java.io.File workingDir
setWorkingDirectory(File)
or setWorkingDirectory(String)
instead.public Commandline(java.lang.String toProcess, Shell shell)
toProcess
- public Commandline(Shell shell)
public Commandline(java.lang.String toProcess)
toProcess
- public Commandline()
public long getPid()
public void setPid(long pid)
private void setDefaultShell()
Sets the shell or command-line interpreter for the detected operating system, and the shell arguments.
public Commandline.Argument createArgument()
createArg()
insteadCreates an argument object.
Each commandline object has at most one instance of the argument class. This method calls
this.createArgument(false)
.
createArgument(boolean)
public Commandline.Argument createArgument(boolean insertAtStart)
createArg(boolean)
insteadCreates an argument object and adds it to our list of args.
Each commandline object has at most one instance of the argument class.
insertAtStart
- if true, the argument is inserted at the beginning of the list of args, otherwise it is
appended.public Arg createArg()
Creates an argument object.
Each commandline object has at most one instance of the argument class. This method calls
this.createArgument(false)
.
createArgument(boolean)
public Arg createArg(boolean insertAtStart)
Creates an argument object and adds it to our list of args.
Each commandline object has at most one instance of the argument class.
insertAtStart
- if true, the argument is inserted at the beginning of the list of args, otherwise it is
appended.public void addArg(Arg argument)
addArg(Arg,boolean)
public void addArg(Arg argument, boolean insertAtStart)
insertAtStart
- if true, the argument is inserted at the beginning of the list of args, otherwise it is
appended.public void setExecutable(java.lang.String executable)
public java.lang.String getLiteralExecutable()
public java.lang.String getExecutable()
public void addArguments(java.lang.String[] line)
public void addEnvironment(java.lang.String name, java.lang.String value)
public void addSystemEnvironment() throws java.lang.Exception
java.lang.Exception
public java.lang.String[] getEnvironmentVariables() throws CommandLineException
CommandLineException
public java.lang.String[] getCommandline()
getShellCommandline()
is returnedpublic java.lang.String[] getRawCommandline()
public java.lang.String[] getShellCommandline()
public java.lang.String[] getArguments()
addLine
, addValue
or the argument object.public java.lang.String toString()
toString
in class java.lang.Object
public int size()
public java.lang.Object clone()
clone
in class java.lang.Object
public void clear()
public void clearArgs()
public Commandline.Marker createMarker()
Return a marker.
This marker can be used to locate a position on the commandline - to insert something for example - when all parameters have been set.
public void setWorkingDirectory(java.lang.String path)
public void setWorkingDirectory(java.io.File workingDirectory)
public java.io.File getWorkingDirectory()
public java.lang.Process execute() throws CommandLineException
CommandLineException
private void verifyShellState()
public java.util.Properties getSystemEnvVars() throws java.lang.Exception
java.lang.Exception
public void setShell(Shell shell)
shell
- public Shell getShell()
public static java.lang.String[] translateCommandline(java.lang.String toProcess) throws java.lang.Exception
CommandLineUtils.translateCommandline(String)
instead.java.lang.Exception
public static java.lang.String quoteArgument(java.lang.String argument) throws CommandLineException
CommandLineUtils.quote(String)
instead.CommandLineException
public static java.lang.String toString(java.lang.String[] line)
CommandLineUtils.toString(String[])
instead.