Run remote command via ssh using Maven3

Dexter picture Dexter · Sep 13, 2012 · Viewed 17.9k times · Source

I am using wagon-maven-plugin to scp my WAR file to the server. It works fine. My next step is to perform some commands on the server (mkdir, etc). Is there a plugin that helps me do that? Is there a way to work it out using wagon-maven-plugin?

I am relatively new to mvn. Any help would be appreciated.

Any suggestions?

Answer

Dexter picture Dexter · Sep 20, 2012

I was able to run ssh commands with exec-maven-plugin. It is a powerful maven plugin to do all sorts of hack and also run commands. For anyone interested in the solution

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>exec-maven-plugin</artifactId>
  <version>1.2.1</version>
  <executions>
    <execution>
      <phase>install</phase>
      <goals>
        <goal>exec</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <executable>sh</executable>
    <arguments>
      <!-- Shell script location -->
      <argument>runscript.sh</argument>
      <!-- arg #1 -->
      <argument>${file_1}</argument>
    </arguments>
  </configuration>
</plugin>

Another solution I found was to run maven-antrun-plugin. I would not recommend it since it runs ANT tasks and there are a lot of dependencies to it. But its handy if you would need to run ant tasks via maven.

<plugin>
  <inherited>false</inherited>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-antrun-plugin</artifactId>
  <version>1.6</version>
  <configuration>
    <target>
      <loadproperties srcFile="deploy.properties" />
      <ftp action="send" server="server"
           remotedir="/a/b" userid="usr"
           password="pw" depends="no"
           verbose="yes" binary="yes">
        <fileset dir="modules/my-module/target">
          <include name="file.zip" />
        </fileset>
      </ftp>

      <!-- calls deploy script -->
      <sshexec host="host" trust="yes"
               username="usr" password="pw"
               command="sh /my/script.sh" />

      <!-- SSH -->
      <taskdef name="sshexec"
               classname="org.apache.tools.ant.taskdefs.optional.ssh.SSHExec"
               classpathref="maven.plugin.classpath" />
      <taskdef name="ftp"
               classname="org.apache.tools.ant.taskdefs.optional.net.FTP"
               classpathref="maven.plugin.classpath" />
    </target>
  </configuration>
  ...
  <dependencies>
    <dependency>
      <groupId>commons-net</groupId>
      <artifactId>commons-net</artifactId>
      <version>1.4.1</version>
    </dependency>
    <dependency>
      <groupId>ant</groupId>
      <artifactId>ant-commons-net</artifactId>
      <version>1.6.5</version>
    </dependency>
    <dependency>
      <groupId>ant</groupId>
      <artifactId>ant-jsch</artifactId>
      <version>1.6.5</version>
    </dependency>
    <dependency>
      <groupId>jsch</groupId>
      <artifactId>jsch</artifactId>
      <version>0.1.29</version>
    </dependency>
  </dependencies>
</plugin>

Hope that helps!