如何在ant文件中執行以下任務?
- 制作zip文件。
- 運行命令。
- 將文件復制到遠程計算機。
- 在遠程Linux機器上運行命令。
- 打開輸入框并響應輸入值。
- 撥打螞蟻電話。
答案:
1.制作zip文件:
以下是在ant中制作zip文件的xml:
<zip destfile='${destination.folder}/zipName.zip'><fileset dir= '${Source.folder}' />
</zip>
在此,“ destfile”是創建的zip文件的名稱和位置。 在文件集標記中,dir屬性用于指定將壓縮所有文件的源文件夾格式。
2.運行命令:
在這里,我將向您展示如何在ant文件中啟動tomcat,以演示如何在ant中運行命令。 以下是為此的xml:
<exec dir='${tomcat.home}/bin' executable='cmd' os='Windows XP'><arg line='/c startup.bat'/>
</exec>
這里的“ $ {tomcat.home}”是tomcat文件夾的路徑。 該命令在“ line”屬性的“ <arg>”標簽中給出。
注意:要運行以下命令,您將需要JSCH jar 。
3.將文件復制到遠程計算機:
如果遠程計算機位于您的局域網中,并且您可以直接以\\ remote_machine \ path對其進行訪問,則可以使用簡單的復制命令,如下所示:
<copy file='${source.folder.file} ' todir='\\remote\path'>
</copy>
要在支持SCP的遠程計算機上復制文件,請使用以下標記:
<scp file='${source.folder.file} ' todir='${remote.user}@${remote.host}:${remote.path.where.to.do.copy}'password='${remote.password}' trust='true'>
</scp>
在以上兩個命令中,“文件”是要復制其路徑的源文件。 “ todir”是要在其中復制文件的遠程計算機文件夾路徑。
4.在遠程計算機上運行命令:
您可以使用以下標記在遠程Linux機器上執行命令。
<sshexec host='${remote.host}' username='${remote.username}' password='${remote.password}'command='${command.to.run}' trust='true' />
例如,要啟動tomcat,請使用以下標記:
<sshexec host='${remote.host}' username='${remote.user}' password='${remote.password}'command='sh ${tomcat.home}/startup.sh' trust='true' />
5.打開一個輸入框并響應輸入值 :
要打開輸入對話框,請使用以下標記:
<input message='Enter id: ' addproperty='my.id'>
</input>
“ addproperty”中的“ my.id”是保存輸入值的變量名。 現在檢查用戶是否拒絕在輸入中輸入值:
<condition property='do.abort'><equals arg1='n' arg2='${my.id}'/>
</condition>
<fail if='do.abort'>Build aborted by user.</fail>
如果用戶輸入值并按OK,則可以將輸入的值稱為“ $ {my.id}”。
6.撥打螞蟻電話 :
<antcall target='targetName'>
</antcall>
這里的“目標”是將要執行的目標的名稱。
注意:在以上示例中,所有以“ $ {”開頭和以“}”結尾的值都是變量,您可能必須在其中輸入適當的值才能成功運行它們。
參考: harryjoy博客上來自JCG合作伙伴 Harsh Raval的有用的Ant構建標簽 。
翻譯自: https://www.javacodegeeks.com/2012/09/useful-ant-build-tags.html