首页 > 代码库 > ant新建scp和sshexec任务

ant新建scp和sshexec任务

1、build.xml中新建targer如下:

    <target name="remotecopytest" description="拷贝文件到远程服务器">
        <scp file="C:\Users\testejb3\Workspaces\MyEclipse 8.5\helloworld\helloworld.jar" todir="root@192.168.152.129:/home/wsc/" trust="true" password="411816"></scp>
        <echo message="上传完毕"></echo>
        <sshexec failonerror="yes" host="192.168.152.129" username="root" password="411816" command="chown wsc:wsc /home/wsc/helloworld.jar" trust="true"></sshexec>
        <echo message="修改文件属主完毕"/>
    </target>

 

2、ant debug环境配置如下,运行

3、运行结果log

Apache Ant version 1.7.1 compiled on June 27 2008
Setting ro project property: ant.file -> C:\Users\testejb3\Workspaces\MyEclipse 8.5\helloworld\build.xml
Buildfile: C:\Users\testejb3\Workspaces\MyEclipse 8.5\helloworld\build.xml
Adding reference: ant.projectHelper
Adding reference: ant.parsing.context
Adding reference: ant.targets
parsing buildfile C:\Users\testejb3\Workspaces\MyEclipse 8.5\helloworld\build.xml with URI = file:/C:/Users/testejb3/Workspaces/MyEclipse%208.5/helloworld/build.xml
Setting ro project property: ant.project.name -> helloworld
Adding reference: helloworld
Setting ro project property: ant.file.helloworld -> C:\Users\testejb3\Workspaces\MyEclipse 8.5\helloworld\build.xml
Project base dir set to: C:\Users\testejb3\Workspaces\MyEclipse 8.5\helloworld
 +Target: 
 +Target: echotest
 +Target: remotecopytest
 +Target: prepare
 +Target: compile
 +Target: ejbjar
 +Target: deploy
 +Target: undeploy
[antlib:org.apache.tools.ant] Could not load definitions from resource org/apache/tools/ant/antlib.xml. It could not be found.
Setting project property: src.dir -> C:\Users\testejb3\Workspaces\MyEclipse 8.5\helloworld\src
 [property] Loading Environment env.
Setting project property: env.TEMP -> C:\Users\testejb3\AppData\Local\Temp
Setting project property: env.ProgramW6432 -> C:\Program Files
Setting project property: env.JAVA_HOME -> C:\Program Files (x86)\Java\jdk1.6.0_32
Setting project property: env.ProgramFiles(x86) -> C:\Program Files (x86)
Setting project property: env.ProgramFiles -> C:\Program Files (x86)
Setting project property: env.Path -> C:/Users/testejb3/AppData/Local/Genuitec/Common/binary/com.sun.java.jdk.win32.x86_1.6.0.013/jre/bin/client;C:/Users/testejb3/AppData/Local/Genuitec/Common/binary/com.sun.java.jdk.win32.x86_1.6.0.013/jre/bin;C:\soft\jboss-4.2.3.GA\bin;C:\Program Files (x86)\Java\jdk1.6.0_32\bin;C:\Program Files\EmEditor;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\
Setting project property: env.PSModulePath -> C:\Windows\system32\WindowsPowerShell\v1.0\ModulesSetting project property: env.SESSIONNAME -> Console
Setting project property: env.SystemRoot -> C:\Windows
Setting project property: env.USERNAME -> testejb3
Setting project property: env.CommonProgramW6432 -> C:\Program Files\Common Files
Setting project property: env.PROCESSOR_LEVEL -> 6
Setting project property: env.FP_NO_HOST_CHECK -> NO
Setting project property: env.USERPROFILE -> C:\Users\testejb3
Setting project property: env.PROCESSOR_REVISION -> 2a07
Setting project property: env.CommonProgramFiles -> C:\Program Files (x86)\Common Files
Setting project property: env.HOMEPATH -> \Users\testejb3
Setting project property: env.PROCESSOR_ARCHITECTURE -> x86
Setting project property: env.OS -> Windows_NT
Setting project property: env.LOCALAPPDATA -> C:\Users\testejb3\AppData\Local
Setting project property: env.ComSpec -> C:\Windows\system32\cmd.exe
Setting project property: env.PATHEXT -> .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
Setting project property: env.PROMPT -> $P$G
Setting project property: env.ALLUSERSPROFILE -> C:\ProgramData
Setting project property: env.windows_tracing_flags -> 3
Setting project property: env.HOMEDRIVE -> C:
Setting project property: env.PROCESSOR_IDENTIFIER -> Intel64 Family 6 Model 42 Stepping 7, GenuineIntel
Setting project property: env.APPDATA -> C:\Users\testejb3\AppData\Roaming
Setting project property: env.NUMBER_OF_PROCESSORS -> 1
Setting project property: env.JBOSS_HOME -> C:\soft\jboss-4.2.3.GA
Setting project property: env.ANT_HOME -> C:\Users\testejb3\AppData\Local\Genuitec\Common\plugins\org.apache.ant_1.7.1.v20090120-1145
Setting project property: env.ProgramData -> C:\ProgramData
Setting project property: env.CommonProgramFiles(x86) -> C:\Program Files (x86)\Common Files
Setting project property: env.PROCESSOR_ARCHITEW6432 -> AMD64
Setting project property: env.SystemDrive -> C:
Setting project property: env.TMP -> C:\Users\testejb3\AppData\Local\Temp
Setting project property: env.USERDOMAIN -> testweblogic-PC
Setting project property: env.windows_tracing_logfile -> C:\BVTBin\Tests\installpackage\csilogfile.log
Setting project property: env.windir -> C:\Windows
Setting project property: env.COMPUTERNAME -> TESTWEBLOGIC-PC
Setting project property: env.PUBLIC -> C:\Users\Public
Setting project property: env.LOGONSERVER -> \\TESTWEBLOGIC-PC
Setting project property: jboss.home -> C:\soft\jboss-4.2.3.GA
Setting project property: jboss.server.config -> default
Setting project property: build.dir -> C:\Users\testejb3\Workspaces\MyEclipse 8.5\helloworld\build
Adding reference: build.classpath
Adding reference: eclipse.ant.targetVector
Build sequence for target(s) `remotecopytest‘ is [remotecopytest]
Complete build sequence is [remotecopytest, echotest, prepare, compile, ejbjar, undeploy, deploy, ]
remotecopytest:
      [scp] Connecting to 192.168.152.129:22
      [scp] done.
     [echo] 上传完毕
  [sshexec] Connecting to 192.168.152.129:22
  [sshexec] cmd : chown wsc:wsc /home/wsc/helloworld.jar
     [echo] 修改文件属主完毕
BUILD SUCCESSFUL
Total time: 3 seconds

4、注意点

运行scp以及sshexec命令需要jscp的jar包,可以从地址:http://prdownloads.sourceforge.net/jsch/jsch-0.1.37.zip?download下载,这个源码,build出来一个jar包jsch-20140509.jar,放到ant的lib目录下面(这个目录信息在ant的出错信息里会有非常详细的提示,这里感觉ant做的很好),我这里用的是MyEclipse自带的ant环境,如下: