Class Commandline

java.lang.Object
org.apache.maven.shared.utils.cli.Commandline
All Implemented Interfaces:
Cloneable

public class Commandline extends Object implements 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.

Author:
thomas.haas@softwired-inc.com, Stefan Bodewig
  • Constructor Details

    • 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(String toProcess) throws CommandLineException
      Create a new command line object. Shell is autodetected from operating system
      Parameters:
      toProcess - The command to process
      Throws:
      CommandLineException
    • Commandline

      public Commandline()
      Create a new command line object. Shell is autodetected from operating system
  • Method Details

    • 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(String executable)
      Sets the executable to run.
      Parameters:
      executable - The executable.
    • getExecutable

      public String getExecutable()
      Returns:
      The executable.
    • addArguments

      public void addArguments(String... line)
      Parameters:
      line - The arguments.
    • addEnvironment

      public void addEnvironment(String name, 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 String[] getEnvironmentVariables()
      Return the list of environment variables
      Returns:
      an array of all environment variables.
    • getCommandline

      public String[] getCommandline()
      Returns the executable and all defined arguments.
      Returns:
      an array of all arguments incl. executable.
    • getArguments

      public String[] getArguments()
      Returns all arguments defined by addLine, addValue or the argument object.
      Returns:
      an array of arguments.
    • getArguments

      public String[] getArguments(boolean mask)
      Returns all arguments defined by addLine, addValue or the argument object.
      Parameters:
      mask - flag to mask any arguments (having his mask field to true).
      Returns:
      an array of arguments.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • clone

      public Object clone()
      Overrides:
      clone in class Object
    • setWorkingDirectory

      public void setWorkingDirectory(String path)
      Sets working directory.
      Parameters:
      path - The to be set as working directory.
    • setWorkingDirectory

      public void setWorkingDirectory(File workingDirectory)
      Sets execution directory.
      Parameters:
      workingDirectory - The working directory.
    • getWorkingDirectory

      public 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 Process execute() throws CommandLineException
      Executes 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.