View Javadoc
1   package org.codehaus.mojo.wagon;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.plugin.MojoExecutionException;
23  import org.apache.maven.plugins.annotations.Mojo;
24  import org.apache.maven.plugins.annotations.Parameter;
25  import org.apache.maven.wagon.CommandExecutor;
26  import org.apache.maven.wagon.Streams;
27  import org.apache.maven.wagon.Wagon;
28  import org.apache.maven.wagon.WagonException;
29  import org.apache.maven.wagon.providers.ssh.jsch.AbstractJschWagon;
30  
31  /**
32   * Executes a list of commands against a given server.
33   */
34  @Mojo( name = "sshexec")
35  public class SshExecMojo
36      extends AbstractSingleWagonMojo
37  {
38  
39      /**
40       * The commands that we will execute.
41       */
42      @Parameter(required = true)
43      private String[] commands;
44  
45      /**
46       * Allow option not to fail the build on error.
47       */
48      @Parameter(defaultValue = "true")
49      private boolean failOnError = true;
50  
51      /**
52       * Option to display remote command's outputs.
53       */
54      @Parameter(defaultValue = "false")
55      private boolean displayCommandOutputs = true;
56  
57      @Override
58      protected void execute( final Wagon wagon )
59          throws MojoExecutionException
60      {
61          if ( commands != null )
62          {
63              for ( String command : commands )
64              {
65                  try
66                  {
67                      Streams stream;
68                      if ( wagon instanceof AbstractJschWagon )
69                      {
70                          stream = ( (AbstractJschWagon) wagon ).executeCommand( command, true, false );
71                      } else
72                      {
73                          stream = ( (CommandExecutor) wagon ).executeCommand( command, false );
74                      }
75                      this.getLog().info( "sshexec: " + command + " ..." );
76                      if ( displayCommandOutputs )
77                      {
78                          System.out.println( stream.getOut() );
79                          System.out.println( stream.getErr() );
80                      }
81                  } catch ( final WagonException e )
82                  {
83                      if ( this.failOnError )
84                      {
85                          throw new MojoExecutionException( "Unable to execute remote command", e );
86                      }
87                      else
88                      {
89                          this.getLog().warn( e );
90                      }
91                  }
92  
93              }
94          }
95      }
96  }