Ant从gradle调用的sshexec任务不显示输出
问题描述:
我想在我的gradle自定义任务中使用Apache ant sshexec
任务。问题是这个任务不起作用(输出未在控制台中显示,并且sshexec操作未执行)。这是我如何使用它:Ant从gradle调用的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那些是我的结果:
-
cmd.exe /C echo test > C:\testresult.txt
从蚂蚁工作正常启动的命令和输出返回文件。 - 从gradle开始的命令
cmd.exe /C echo test > C:\testresult.txt
正常工作,输出返回到文件。大! - 从ant开始的命令
cmd.exe /C echo test
正常工作,输出返回到stdout。 ! - 从gradle开始的命令
cmd.exe /C echo test
正常工作,但输出不会返回到stdout。 ! -
cmd.exe /C mkdir C:\\\\Inetpub\\\\ftproot\\\\temp\\\\jakisnowykatalog
从蚂蚁工作正常启动的命令,并创建目录(我需要使用\\\\
作为路径分隔符,因为\\
,\
,/
不工作) -
cmd.exe /C mkdir C:\\\\Inetpub\\\\ftproot\\\\temp\\\\jakisnowykatalog
从开始的gradle的命令不工作,目录没有被创建。
我应该补充说我想用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()
}
}
}
答
假设你声明类型MyCustomTask的任务,并正确地执行它,我看不出有任何理由Ant任务将不会得到执行。问题在其他地方更可能发生(例如,Ant任务的配置错误)。
谢谢,我会检查它。我确信蚂蚁能正常工作,因为我每天都在使用它。 – pepuch 2013-05-15 05:10:10