1、build.xml中新建targer如下:
<target name="remotecopytest" description="拷贝文件到远程服务器"> <scp file="C:Users estejb3WorkspacesMyEclipse 8.5helloworldhelloworld.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 estejb3WorkspacesMyEclipse 8.5helloworlduild.xml Buildfile: C:Users estejb3WorkspacesMyEclipse 8.5helloworlduild.xml Adding reference: ant.projectHelper Adding reference: ant.parsing.context Adding reference: ant.targets parsing buildfile C:Users estejb3WorkspacesMyEclipse 8.5helloworlduild.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 estejb3WorkspacesMyEclipse 8.5helloworlduild.xml Project base dir set to: C:Users estejb3WorkspacesMyEclipse 8.5helloworld +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 estejb3WorkspacesMyEclipse 8.5helloworldsrc [property] Loading Environment env. Setting project property: env.TEMP -> C:Users estejb3AppDataLocalTemp Setting project property: env.ProgramW6432 -> C:Program Files Setting project property: env.JAVA_HOME -> C:Program Files (x86)Javajdk1.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:softjboss-4.2.3.GAin;C:Program Files (x86)Javajdk1.6.0_32in;C:Program FilesEmEditor;C:Windowssystem32;C:Windows;C:WindowsSystem32Wbem;C:WindowsSystem32WindowsPowerShellv1.0 Setting project property: env.PSModulePath -> C:Windowssystem32WindowsPowerShellv1.0Modules Setting 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 FilesCommon Files Setting project property: env.PROCESSOR_LEVEL -> 6 Setting project property: env.FP_NO_HOST_CHECK -> NO Setting project property: env.USERPROFILE -> C:Users estejb3 Setting project property: env.PROCESSOR_REVISION -> 2a07 Setting project property: env.CommonProgramFiles -> C:Program Files (x86)Common Files Setting project property: env.HOMEPATH -> Users estejb3 Setting project property: env.PROCESSOR_ARCHITECTURE -> x86 Setting project property: env.OS -> Windows_NT Setting project property: env.LOCALAPPDATA -> C:Users estejb3AppDataLocal Setting project property: env.ComSpec -> C:Windowssystem32cmd.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 estejb3AppDataRoaming Setting project property: env.NUMBER_OF_PROCESSORS -> 1 Setting project property: env.JBOSS_HOME -> C:softjboss-4.2.3.GA Setting project property: env.ANT_HOME -> C:Users estejb3AppDataLocalGenuitecCommonpluginsorg.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 estejb3AppDataLocalTemp Setting project property: env.USERDOMAIN -> testweblogic-PC Setting project property: env.windows_tracing_logfile -> C:BVTBinTestsinstallpackagecsilogfile.log Setting project property: env.windir -> C:Windows Setting project property: env.COMPUTERNAME -> TESTWEBLOGIC-PC Setting project property: env.PUBLIC -> C:UsersPublic Setting project property: env.LOGONSERVER -> \TESTWEBLOGIC-PC Setting project property: jboss.home -> C:softjboss-4.2.3.GA Setting project property: jboss.server.config -> default Setting project property: build.dir -> C:Users estejb3WorkspacesMyEclipse 8.5helloworlduild 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环境,如下: