Class Commandline
- java.lang.Object
-
- org.apache.maven.shared.utils.cli.Commandline
-
- All Implemented Interfaces:
java.lang.Cloneable
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>
The element
<acommandline executable="/executable/to/run">
<argument value="argument 1" />
<argument line="argument_1 argument_2 argument_3" />
<argument value="argument 4" />
</acommandline>
</someelement>
someelement
must provide a methodcreateAcommandline
which returns an instance of this class.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Commandline.Argument
-
Constructor Summary
Constructors Constructor Description Commandline()
Create a new command line object.Commandline(java.lang.String toProcess)
Create a new command line object.Commandline(Shell shell)
Create a new command line object.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addArguments(java.lang.String... line)
void
addEnvironment(java.lang.String name, java.lang.String value)
Add an environment variablevoid
addSystemEnvironment()
Add system environment variablesvoid
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.java.lang.Process
execute()
Executes the command.java.lang.String[]
getArguments()
Returns all arguments defined byaddLine
,addValue
or the argument object.java.lang.String[]
getArguments(boolean mask)
Returns all arguments defined byaddLine
,addValue
or the argument object.java.lang.String[]
getCommandline()
Returns the executable and all defined arguments.java.lang.String[]
getEnvironmentVariables()
Return the list of environment variablesjava.lang.String
getExecutable()
Shell
getShell()
Get the shell to be used in this command line.private java.lang.String[]
getShellCommandline()
private java.lang.String[]
getShellCommandline(boolean mask)
java.io.File
getWorkingDirectory()
private void
setDefaultShell()
Sets the shell or command-line interpretor for the detected operating system, and the shell arguments.void
setExecutable(java.lang.String executable)
Sets the executable to run.(package private) 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 working directory.java.lang.String
toString()
-
-
-
Constructor Detail
-
Commandline
public Commandline(Shell shell)
Create a new command line object. Shell is autodetected from operating system- Parameters:
shell
- The shell instance.
-
Commandline
public Commandline(java.lang.String toProcess)
Create a new command line object. Shell is autodetected from operating system- Parameters:
toProcess
- The command to process
-
Commandline
public Commandline()
Create a new command line object. Shell is autodetected from operating system
-
-
Method Detail
-
setDefaultShell
private void setDefaultShell()
Sets the shell or command-line interpretor for the detected operating system, and the shell arguments.
-
createArg
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)
.- Returns:
- the argument object.
-
createArg
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.
- Parameters:
insertAtStart
- if true, the argument is inserted at the beginning of the list of args, otherwise it is appended.- Returns:
- The arguments.
-
setExecutable
public void setExecutable(java.lang.String executable)
Sets the executable to run.- Parameters:
executable
- The executable.
-
getExecutable
public java.lang.String getExecutable()
- Returns:
- The executable.
-
addArguments
public void addArguments(java.lang.String... line)
- Parameters:
line
- The arguments.
-
addEnvironment
public void addEnvironment(java.lang.String name, java.lang.String value)
Add an environment variable- Parameters:
name
- The name of the environment variable.value
- The appropriate value.
-
addSystemEnvironment
public void addSystemEnvironment()
Add system environment variables
-
getEnvironmentVariables
public java.lang.String[] getEnvironmentVariables()
Return the list of environment variables- Returns:
- an array of all environment variables.
-
getCommandline
public java.lang.String[] getCommandline()
Returns the executable and all defined arguments.- Returns:
- an array of all arguments incl. executable.
-
getShellCommandline
private java.lang.String[] getShellCommandline()
- Returns:
- the shell, executable and all defined arguments without masking any arguments.
-
getShellCommandline
private java.lang.String[] getShellCommandline(boolean mask)
- Parameters:
mask
- flag to mask any arguments (having hismask
field totrue
).- Returns:
- the shell, executable and all defined arguments with masking some arguments if
mask
parameter is on
-
getArguments
public java.lang.String[] getArguments()
Returns all arguments defined byaddLine
,addValue
or the argument object.- Returns:
- an array of arguments.
-
getArguments
public java.lang.String[] getArguments(boolean mask)
Returns all arguments defined byaddLine
,addValue
or the argument object.- Parameters:
mask
- flag to mask any arguments (having hismask
field totrue
).- Returns:
- an array of arguments.
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
clone
public java.lang.Object clone()
- Overrides:
clone
in classjava.lang.Object
-
setWorkingDirectory
public void setWorkingDirectory(java.lang.String path)
Sets working directory.- Parameters:
path
- The to be set as working directory.
-
setWorkingDirectory
public void setWorkingDirectory(java.io.File workingDirectory)
Sets execution directory.- Parameters:
workingDirectory
- The working directory.
-
getWorkingDirectory
public java.io.File getWorkingDirectory()
- Returns:
- The working directory.
-
clearArgs
public void clearArgs()
Clear out the arguments but leave the executable in place for another operation.
-
execute
public java.lang.Process execute() throws CommandLineException
Executes the command.- Returns:
- The process.
- Throws:
CommandLineException
- in case of errors.
-
setShell
void setShell(Shell shell)
Allows to set the shell to be used in this command line.- Parameters:
shell
- the shell
-
getShell
public Shell getShell()
Get the shell to be used in this command line.- Returns:
- the shell.
-
-