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.CloneableCommandline 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
someelementmust provide a methodcreateAcommandlinewhich returns an instance of this class.- Author:
- thomas.haas@softwired-inc.com, Stefan Bodewig
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classCommandline.ArgumentA single command line 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 Deprecated Methods Modifier and Type Method Description voidaddArguments(java.lang.String... line)voidaddEnvironment(java.lang.String name, java.lang.String value)Add an environment variable.voidaddSystemEnvironment()Deprecated.please usesetShellEnvironmentInherited(boolean)voidclearArgs()Clear out the arguments but leave the executable in place for another operation.java.lang.Objectclone()ArgcreateArg()Creates an empty argument object and inserts it at the end of the argument list.ArgcreateArg(boolean insertAtStart)Creates an argument object and adds it to the list of args.java.lang.Processexecute()Execute the command.java.lang.String[]getArguments()Returns all arguments defined byaddLine,addValueor 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 variables.java.lang.StringgetExecutable()ShellgetShell()Get the shell to be used in this command line.java.io.FilegetWorkingDirectory()booleanisShellEnvironmentInherited()Indicates whether the environment variables of the current process should are propagated to the executing Command.voidsetExecutable(java.lang.String executable)Sets the executable to run.voidsetShellEnvironmentInherited(boolean shellEnvironmentInherited)Specifies whether the environment variables of the current process should be propagated to the executing Command.voidsetWorkingDirectory(java.io.File workingDirectory)Sets working directory.voidsetWorkingDirectory(java.lang.String path)Sets working directory.java.lang.StringtoString()
-
-
-
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) throws CommandLineExceptionCreate a new command line object. Shell is autodetected from operating system.- Parameters:
toProcess- the command to process- Throws:
CommandLineException- in case of unbalanced quotes.
-
Commandline
public Commandline()
Create a new command line object. Shell is autodetected from operating system.
-
-
Method Detail
-
createArg
public Arg createArg()
Creates an empty argument object and inserts it at the end of the argument list.- Returns:
- the argument object
-
createArg
public Arg createArg(boolean insertAtStart)
Creates an argument object and adds it to the list of args.- Parameters:
insertAtStart- if true, the argument is inserted at the beginning of the list of args. Otherwise it is appended.- Returns:
- the argument
-
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 variablevalue- the appropriate value
-
addSystemEnvironment
@Deprecated public void addSystemEnvironment()
Deprecated.please usesetShellEnvironmentInherited(boolean)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 including the executable
-
getArguments
public java.lang.String[] getArguments()
Returns all arguments defined byaddLine,addValueor 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 hismaskfield totrue)- Returns:
- an array of arguments
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
clone
public java.lang.Object clone()
- Overrides:
clonein classjava.lang.Object
-
setWorkingDirectory
public void setWorkingDirectory(java.lang.String path)
Sets working directory.- Parameters:
path- the working directory
-
setWorkingDirectory
public void setWorkingDirectory(java.io.File workingDirectory)
Sets working 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.
-
isShellEnvironmentInherited
public boolean isShellEnvironmentInherited()
Indicates whether the environment variables of the current process should are propagated to the executing Command. By default, the current environment variables are inherited by the new Command line execution.- Returns:
trueif the environment variables should be propagated,falseotherwise.
-
setShellEnvironmentInherited
public void setShellEnvironmentInherited(boolean shellEnvironmentInherited)
Specifies whether the environment variables of the current process should be propagated to the executing Command.- Parameters:
shellEnvironmentInherited-trueif the environment variables should be propagated,falseotherwise.
-
execute
public java.lang.Process execute() throws CommandLineExceptionExecute the command.- Returns:
- the process
- Throws:
CommandLineException- in case of errors
-
getShell
public Shell getShell()
Get the shell to be used in this command line.- Returns:
- the shell
-
-