Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
873 views
in Technique[技术] by (71.8m points)

maven 3 - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...