1. 程式人生 > 實用技巧 >ANT之macrodef

ANT之macrodef

macrodef 的意思是巨集定義, 可以理解為自定義函式。

對於大型部署,可以提高程式碼利用率。

為了方便理解,請看程式碼示例:

<macrodef name="macro-send-file">
  		<attribute name="host" />
		<attribute name="toPath" />
		<attribute name="file" />
		<attribute name="userID" />
		<attribute name="password" />
<sequential>
<echo>@{userID}:@{password}@@@{host}:@{toPath}</echo>
<scp verbose="false" trust="true" localFile="@{file}" todir="@{userID}:@{password}@@@{host}:@{toPath}" failonerror="true" />
<!-- sshexec trust="true" host="@{host}" username="@{userID}" password="@{password}" command="chmod -R u+rw,g+rwx @{toPath}/*.ear"/ --><sshexec trust="true" host="@{host}" username="@{userID}" password="@{password}" command="chown thangamm:wadmfsms  @{toPath}/*.ear"/>
<echo>Sent @{file} to @{host}:@{toPath}</echo>
			
<!-- delete build dir -->
<echo message="clean app project" />
<!-- after done, should clean the preparation folders but keep release folder -->		
<delete dir="${build.dir.app}${file.separator}${name.bin}" failonerror="false" />
<delete dir="${build.dir.app}${file.separator}${name.docs}" failonerror="false" />
<delete dir="${build.dir.app}${file.separator}${name.reports}" failonerror="false" />
<delete dir="${build.dir.app}${file.separator}${name.stage}" failonerror="false" />
		
			<echo message="clean core project" />
			<delete dir="${build.dir.core}" failonerror="false" />		
			
			<echo message="clean web project" />
			<delete dir="${build.dir.web}" failonerror="false" />				
		</sequential>		
	</macrodef>

name 屬性說明這個自定義巨集的名字 通過這個name 來呼叫這個巨集

<macro-send-file file="${file}" host="${autodeploy.host}" topath="${autodeploy.path}/${envName}" userid="${autodeploy.userID}" password="${autodeploy.password}" />

表示巨集的輸入引數(相當於函式的引數)。在marcodef 中不同通過${}來呼叫attrbute 的值而要通過@{}。例如顯示傳入引數userID 是什麼通過@{userID}.

相當於函式的body 就是你這個可以呼叫常用ant tag 如何copy javac mkdir jar war 等

補充連結

http://lhq1013.iteye.com/blog/1157234