2

sshexecgradle カスタム タスクでApache ant タスクを使用したいと考えています。問題は、このタスクが機能しないことです (コンソールに出力が表示されず、sshexec アクションが実行されません)。これは私がそれを使用する方法です:

configurations {
    sshexecAntTask
}

repositories {
    mavenCentral()
}

dependencies {
    sshexecAntTask 'org.apache.ant:ant-jsch:1.7.0'
}

// ----------------------------------------------------

import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files

class MyCustomTask extends DefaultTask {

    @TaskAction
    def build() {
        String command = ""
        command = 'cmd.exe /C mdir C:\\aadd'
        runSshCommand(command)
    }

    private void runSshCommand(String command) {
        String host = "host"
        String username = "username"
        String password = "password"

        ant.taskdef(name: 'sshexec', classname: 'org.apache.tools.ant.taskdefs.optional.ssh.SSHExec', classpath: project.configurations.sshexecAntTask.asPath)
        // this command is not executed; why?
        ant.sshexec(host: host, username: username, password: password, command: command, trust: 'true', failonerror: 'true')
    }

}

[編集] 私は sshexec をテストしましたが、それらは私の結果です:

  1. ant から開始されたコマンドcmd.exe /C echo test > C:\testresult.txtは正しく機能し、出力はファイルに返されます。
  2. cmd.exe /C echo test > C:\testresult.txtgradle から開始されたコマンドは正しく機能し、出力はファイルに返されます。すごい!
  3. ant から開始されたコマンドcmd.exe /C echo testは正しく機能し、出力は stdout に返されます。!
  4. cmd.exe /C echo testgradle から開始されたコマンドは正しく機能しますが、出力は stdout に返されません。!
  5. ant から開始されたコマンドcmd.exe /C mkdir C:\\\\Inetpub\\\\ftproot\\\\temp\\\\jakisnowykatalogは正しく機能し、ディレクトリが作成されます ( , , が機能しないため、パス区切りとして使用する必要があります\\\\)\\\/
  6. gradle から開始したコマンドcmd.exe /C mkdir C:\\\\Inetpub\\\\ftproot\\\\temp\\\\jakisnowykatalogが機能せず、ディレクトリが作成されません。

Windows sshサーバー(unix / macではなく)に接続したいことを追加する必要がありますが、これらのコマンドをmac shhでテストしても成功しませんでした。助けてください!

[別の編集] jschライブラリを使用してコマンドを実行するグ​​ルーヴィーなテストコードを作成しましたが、動作します。ant タスクが機能しない理由はまだわかりません。

import com.jcraft.jsch.*
import java.util.Properties;

private void jschTest() {
    Session session = null
    Channel channel = null
    try {
        JSch jsch = new JSch()
        session = jsch.getSession("host", "login", 22)
        session.setPassword("password")
        Properties config = new Properties()
        config.put("StrictHostKeyChecking", "no")
        session.setConfig(config)
        session.connect()

        String command = "cmd.exe /C mkdir C:\\gradledir"
        channel = session.openChannel("exec");
        ((ChannelExec)channel).setCommand(command);
        channel.connect()
    }
    catch (Exception e) {
        println e.getMessage()
    }
    finally {
        if (session!=null) {
            session.disconnect()
        }
        if (channel!=null) {
            channel.disconnect()
        }
    }
}
4

1 に答える 1