public void execute ( ) throws BuildException { if ( getHost ( ) == null ) { throw new BuildException ( "Host is required." ) ; } if ( getUserInfo ( ) . getName ( ) == null ) { throw new BuildException ( "Username is required." ) ; } if ( getUserInfo ( ) . getKeyfile ( ) == null && getUserInfo ( ) . getPassword ( ) == null && getSshKeyData ( ) == null ) { throw new BuildException ( "Password or Keyfile is required." ) ; } if ( command == null && commandResource == null ) { throw new BuildException ( "Command or commandResource is required." ) ; } if ( inputFile != null && inputProperty != null ) { throw new BuildException ( "You can't specify both inputFile and" + " inputProperty." ) ; } if ( inputFile != null && ! inputFile . exists ( ) ) { throw new BuildException ( "The input file " + inputFile . getAbsolutePath ( ) + " does not exist." ) ; } Session session = null ; StringBuffer output = new StringBuffer ( ) ; try { session = openSession ( ) ; if ( null != getDisconnectHolder ( ) ) { final Session sub = session ; getDisconnectHolder ( ) . setDisconnectable ( new Disconnectable ( ) { public void disconnect ( ) { sub . disconnect ( ) ; } } ) ; } /* called once */ if ( command != null ) { executeCommand ( session , command , output ) ; } else { // read command resource and execute for each command try { BufferedReader br = new BufferedReader ( new InputStreamReader ( commandResource . getInputStream ( ) ) ) ; String cmd ; while ( ( cmd = br . readLine ( ) ) != null ) { executeCommand ( session , cmd , output ) ; output . append ( "\n" ) ; } FileUtils . close ( br ) ; } catch ( IOException e ) { if ( getFailonerror ( ) ) { throw new BuildException ( e ) ; } else { log ( "Caught exception: " + e . getMessage ( ) , Project . MSG_ERR ) ; } } } } catch ( JSchException e ) { if ( getFailonerror ( ) ) { throw new BuildException ( e ) ; } else { log ( "Caught exception: " + e . getMessage ( ) , Project . MSG_ERR ) ; } } finally { try { if ( null != this . sshAgentProcess ) { this . sshAgentProcess . stopAgent ( ) ; } } catch ( IOException e ) { log ( "Caught exception: " + e . getMessage ( ) , Project . MSG_ERR ) ; } if ( outputProperty != null ) { getProject ( ) . setNewProperty ( outputProperty , output . toString ( ) ) ; } if ( session != null && session . isConnected ( ) ) { session . disconnect ( ) ; } } }
Execute the command on the remote host .