【问题标题】:Run remote command via ssh using Maven3使用 Maven3 通过 ssh 运行远程命令
【发布时间】:2012-09-13 18:28:02
【问题描述】:

我正在使用 wagon-maven-plugin 将我的 WAR 文件 scp 到服务器。它工作正常。我的下一步是在服务器上执行一些命令(mkdir 等)。有没有一个插件可以帮助我做到这一点?有没有办法使用 wagon-maven-plugin 解决它?

我对 mvn 比较陌生。任何帮助将不胜感激。

有什么建议吗?

【问题讨论】:

    标签: maven-3 maven-wagon-plugin


    【解决方案1】:

    我能够使用 exec-maven-plugin 运行 ssh 命令。它是一个强大的 Maven 插件,可以进行各种 hack 并运行命令。对于任何对解决方案感兴趣的人

    <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>
    

    我发现的另一个解决方案是运行 maven-antrun-plugin。我不推荐它,因为它运行 ANT 任务并且对它有很多依赖。但如果您需要通过 maven 运行 ant 任务,它会很方便。

    <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>
    

    希望有帮助!

    【讨论】:

    • 另一个选项是 wagon-maven-plugin 的 sshexec 功能
    • 很好,但是 shell 脚本中的内容,也有助于查看这些命令。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-15
    • 2022-01-08
    • 2011-10-26
    • 1970-01-01
    相关资源
    最近更新 更多