Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run remote command via ssh using Maven3

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?

like image 761
Dexter Avatar asked Sep 13 '12 18:09

Dexter


People also ask

How do you connect to a remote server and execute some commands?

Syntax for running commands on a remote Linux or Unix hostssh : The ssh (or other SSH client) is a program for logging into a remote machine and for executing commands on a remote machine. USER-NAME : Remote host user name. REMOTE-HOST : Remote host ip-address or host name, such as fbsd.cyberciti.biz.


1 Answers

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!

like image 58
Dexter Avatar answered Oct 24 '22 01:10

Dexter