FileDocCategorySizeDatePackage
P4Base.javaAPI DocApache Ant 1.709574Wed Dec 13 06:16:20 GMT 2006org.apache.tools.ant.taskdefs.optional.perforce

P4Base

public abstract class P4Base extends org.apache.tools.ant.Task
Base class for Perforce (P4) ANT tasks. See individual task for example usage.
see
P4Sync
see
P4Have
see
P4Change
see
P4Edit
see
P4Submit
see
P4Label
see
org.apache.tools.ant.taskdefs.Execute

Fields Summary
protected org.apache.oro.text.perl.Perl5Util
util
Perl5 regexp in Java - cool eh?
protected String
shell
The OS shell to use (cmd.exe or /bin/sh)
protected String
P4Port
Perforce Server Port (eg KM01:1666)
protected String
P4Client
Perforce Client (eg myclientspec)
protected String
P4User
Perforce User (eg fbloggs)
protected String
P4View
Perforce view for commands. (eg //projects/foobar/main/source/... )
protected boolean
failOnError
Keep going or fail on error - defaults to fail.
protected String
P4Opts
Perforce 'global' opts. Forms half of low level API
protected String
P4CmdOpts
Perforce command opts. Forms half of low level API
private boolean
inError
Set by the task or a handler to indicate that the task has failed. BuildExceptions can also be thrown to indicate failure.
private String
errorMessage
If inError is set, then errorMessage needs to contain the reason why.
Constructors Summary
Methods Summary
protected voidexecP4Command(java.lang.String command)
no usages found for this method runs a Perforce command without a handler

param
command the command that one wants to execute
throws
BuildException if failonerror is set and the command fails

        execP4Command(command, null);
    
protected voidexecP4Command(java.lang.String command, P4Handler handler)
Execute P4 command assembled by subclasses.

param
command The command to run
param
handler A P4Handler to process any input and output
throws
BuildException if failonerror has been set to true

        try {
            // reset error flags before executing the command
            inError = false;
            errorMessage = "";
            Commandline commandline = new Commandline();
            commandline.setExecutable("p4");

            //Check API for these - it's how CVS does it...
            if (P4Port != null && P4Port.length() != 0) {
                commandline.createArgument().setValue(P4Port);
            }
            if (P4User != null && P4User.length() != 0) {
                commandline.createArgument().setValue(P4User);
            }
            if (P4Client != null && P4Client.length() != 0) {
                commandline.createArgument().setValue(P4Client);
            }
            if (P4Opts != null && P4Opts.length() != 0) {
                commandline.createArgument().setLine(P4Opts);
            }
            commandline.createArgument().setLine(command);

            log(commandline.describeCommand(), Project.MSG_VERBOSE);

            if (handler == null) {
                handler = new SimpleP4OutputHandler(this);
            }

            Execute exe = new Execute(handler, null);

            exe.setAntRun(getProject());

            exe.setCommandline(commandline.getCommandline());

            try {
                exe.execute();

                if (inError && failOnError) {
                    throw new BuildException(errorMessage);
                }
            } catch (IOException e) {
                throw new BuildException(e);
            } finally {
                try {
                    handler.stop();
                } catch (Exception e) {
                    log(e.toString(), Project.MSG_ERR);
                }
            }


        } catch (Exception e) {
            String failMsg = "Problem exec'ing P4 command: " + e.getMessage();
            if (failOnError) {
                throw new BuildException(failMsg);
            } else {
                log(failMsg, Project.MSG_ERR);
            }

        }
    
public java.lang.StringgetErrorMessage()
gets the error message recorded by the Perforce handler

return
error message

        return errorMessage;
    
public booleangetInError()
gets whether or not the task has encountered an error

return
error flag
since
ant 1.6


    // CheckStyle:MemberNameCheck ON
    // CheckStyle:VisibilityModifier ON

                         
       
        return inError;
    
public voidinit()
sets attributes Port, Client, User from properties if these properties are defined. Called automatically by UnknownElement

see
org.apache.tools.ant.UnknownElement
PropertyAttribute
p4.portPort
p4.clientClient
p4.userUser


        util = new Perl5Util();

        //Get default P4 settings from environment - Mark would have done something cool with
        //introspection here.....:-)
        String tmpprop;
        if ((tmpprop = getProject().getProperty("p4.port")) != null) {
            setPort(tmpprop);
        }
        if ((tmpprop = getProject().getProperty("p4.client")) != null) {
            setClient(tmpprop);
        }
        if ((tmpprop = getProject().getProperty("p4.user")) != null) {
            setUser(tmpprop);
        }
    
public voidsetClient(java.lang.String p4Client)
The p4 client spec to use; optional, defaults to the current user

param
p4Client the name of the Perforce client spec

        this.P4Client = "-c" + p4Client;
    
public voidsetCmdopts(java.lang.String p4CmdOpts)
Set extra command options; only used on some of the Perforce tasks.

param
p4CmdOpts command line options going after the particular Perforce command

        this.P4CmdOpts = p4CmdOpts;
    
public voidsetErrorMessage(java.lang.String errorMessage)
sets the error message

param
errorMessage line of error output

        this.errorMessage = errorMessage;
    
public voidsetFailonerror(boolean fail)
whether to stop the build (true, default) or keep going if an error is returned from the p4 command

param
fail indicates whether one wants to fail the build if an error comes from the Perforce command

        failOnError = fail;
    
public voidsetGlobalopts(java.lang.String p4Opts)
Set global P4 options; Used on all of the Perforce tasks.

param
p4Opts global options, to use a specific P4Config file for instance

        this.P4Opts = p4Opts;
    
public voidsetInError(boolean inError)
sets the error flag on the task

param
inError if true an error has been encountered by the handler
since
ant 1.6

        this.inError = inError;
    
public voidsetPort(java.lang.String p4Port)
The p4d server and port to connect to; optional, default "perforce:1666"

param
p4Port the port one wants to set such as localhost:1666

        this.P4Port = "-p" + p4Port;
    
public voidsetUser(java.lang.String p4User)
The p4 username; optional, defaults to the current user

param
p4User the user name

        this.P4User = "-u" + p4User;
    
public voidsetView(java.lang.String p4View)
The client, branch or label view to operate upon; optional default "//...". the view is required for the following tasks :
  • p4delete
  • p4edit
  • p4reopen
  • p4resolve

param
p4View the view one wants to use

        this.P4View = p4View;