【问题标题】:Ant sshexec task called from gradle doesn't show output从 gradle 调用的 Ant sshexec 任务不显示输出
【发布时间】:2013-05-14 10:35:08
【问题描述】:

我想在我的 gradle 自定义任务中使用 Apache ant sshexec 任务。问题是此任务不起作用(控制台中未显示输出并且未执行 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. 从 gradle 启动的命令cmd.exe /C echo test > C:\testresult.txt 工作正常,输出返回到文件。太好了!
  3. 从ant 启动的命令cmd.exe /C echo test 工作正常,输出返回到stdout。
  4. 从 gradle 启动的命令 cmd.exe /C echo test 工作正常,但输出未返回到标准输出。
  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 测试了这些命令,但没有成功。请帮忙!

[另一个编辑] 我创建了 groovy 测试代码,它使用 jsch 库来执行命令并且它可以工作。我还是不知道为什么蚂蚁任务不起作用。

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()
        }
    }
}

【问题讨论】:

    标签: java groovy gradle


    【解决方案1】:

    假设您声明了一个 MyCustomTask 类型的任务并正确执行它,我看不出 Ant 任务不执行的原因。问题更可能出现在其他地方(例如 Ant 任务的错误配置)。

    【讨论】:

    • 谢谢,我去看看。我确信 ant 可以正常工作,因为我每天都在使用它。
    猜你喜欢
    • 1970-01-01
    • 2012-10-16
    • 1970-01-01
    • 2010-12-05
    • 1970-01-01
    • 2012-12-05
    • 1970-01-01
    • 1970-01-01
    • 2013-03-16
    相关资源
    最近更新 更多