开发时,常常需要将文件copy到远程服务器,以前都是手工copy:打开scp客户端,连接上远程服务器,然后将本地文件一个一个或者一批一批的拖到远程服务器上。
这样做太麻烦,所以用ant.
可是一般ant只是用来将本地文件拷来拷去,像下面这样:
能不能将文件copy到远程呢?
能,用scpcopy,像这样
这样你就不用手工copy了,只需要运行 ant 命令就行了。
但你可能会碰到这个问题:
D:\workspace\websocket_server\signalserver>ant scpcopy
Buildfile: D:\workspace\websocket_server\signalserver\build.xml
scpcopy:
BUILD FAILED
D:\workspace\websocket_server\signalserver\build.xml:15: Problem: failed to create task or type scp
Cause: Could not load a dependent class com/jcraft/jsch/Logger
It is not enough to have Ant's optional JARs
you need the JAR files that the optional tasks depend upon.
Ant's optional task dependencies are listed in the manual.
Action: Determine what extra JAR files are needed, and place them in one of:
-C:\apache-ant-1.9.6\lib
-C:\Users\qingjieh\.ant\lib
-a directory added on the command line with the -lib argument
Do not panic, this is a common problem.
The commonest cause is a missing JAR.
This is not a bug; it is a configuration problem
Total time: 0 seconds
这是因为你没有将jsch-0.1.55.jar 下载下来放到到C:\apache-ant-1.9.6\lib\里
接着你又可能会碰到这个问题:
D:\workspace\websocket_server\signalserver\build.xml:15: com.jcraft.jsch.JSchException: reject HostKey: 10.133.185.82
at com.jcraft.jsch.Session.checkHost(Session.java:799)
at com.jcraft.jsch.Session.connect(Session.java:345)
at com.jcraft.jsch.Session.connect(Session.java:183)
at org.apache.tools.ant.taskdefs.optional.ssh.SSHBase.openSession(SSHBase.java:225)
at org.apache.tools.ant.taskdefs.optional.ssh.Scp.upload(Scp.java:348)
at org.apache.tools.ant.taskdefs.optional.ssh.Scp.execute(Scp.java:244)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:293)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.Target.execute(Target.java:435)
at org.apache.tools.ant.Target.performTasks(Target.java:456)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1405)
at org.apache.tools.ant.Project.executeTarget(Project.java:1376)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
at org.apache.tools.ant.Project.executeTargets(Project.java:1260)
at org.apache.tools.ant.Main.runBuild(Main.java:853)
at org.apache.tools.ant.Main.startAnt(Main.java:235)
at org.apache.tools.ant.launch.Launcher.run(Launcher.java:285)
at org.apache.tools.ant.launch.Launcher.main(Launcher.java:112)
Total time: 0 seconds
那是因为你没有设置trust="true",咱们给它加上。
运行,一切OK.
D:\workspace\websocket_server\signalserver>ant scpcopy
Buildfile: D:\workspace\websocket_server\signalserver\build.xml
scpcopy:
[scp] Connecting to 192.168.0.99:22
[scp] done.
BUILD SUCCESSFUL
Total time: 1 second