ant扩展之if,for

Ant-contrib是使用Ant编写脚本最重要的补充。当使用Ant编写一些较为复杂的逻辑功能,比如循环和流程判断时,自然希望 Ant 能支持这种编程能力。然而 Ant 核心任务中并没有提供 <if> 任务,只是在 <target> 任务的属性中支持 if 属性,比如 <target name="targetA" if="property-A-present"/>,即表示只有 property-A-present 属性存在才执行targetA 目标。但是,必须注意的一点是,这里的 if 并不是判断 module-A-present 属性是否设置为特定值,而仅仅是检查该属性是否被设置了,因而其可编程性并不是很强。 


demo:

build.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project  default="main">
	<property file="./build.properties" />
	<!--//使用taskdef定制ant-contrib任务 -->
	<taskdef resource="net/sf/antcontrib/antlib.xml">
		<classpath>
			<pathelement location="./lib/ant-contrib-1.0b3.jar" />
		</classpath>
	</taskdef>

	<target name="init">
		<delete dir="../workspace" />
		<mkdir dir="../workspace" />
	</target>

	<target name="condition">
		<if>
			<!--//条件判断,判断arg1 与 arg2两者的值是否相符, 其中arg2为从build.properties中取出的值 -->

			<equals arg1="true" arg2="${compile.arg}" />
			<then>
				<!-- //若符合判断条件,即条件成功,使用antcall命令来执行某个target-->

				<antcall target="compile.codes.main" />
			</then>

			<elseif>
				<equals arg1="${compile.arg}" arg2="false" />
				<then>
					<antcall target="compile.codes.fb" />
				</then>
			</elseif>

			<else>
				<!-- //若所判断条件没有成功,则输出提示信息.-->
				<echo message="The value of the compile.arg can not be matched." />
			</else>
		</if>
	</target>

	<target name="compile.codes.main">
		<for list="${compile.codes.main}" param="mainfile">
			<sequential>
				<echo>compile.codes.main....@{mainfile}</echo>
			</sequential>
		</for>
	</target>

	<target name="compile.codes.fb">
		<for list="${compile.codes.fb}" param="fbfile">
			<sequential>
				<if>
					<equals arg1="fb3.c" arg2="@{fbfile}" />
					<then>
						<!-- //若符合判断条件,即条件成功,使用antcall命令来执行某个target-->

						<antcall target="invoke" />
					</then>
				</if>
				<echo>compile.codes.fb... @{fbfile}</echo>
			</sequential>
		</for>
	</target>
	<target name="invoke">
		<echo>compile.codes.fb... @{fbfile}</echo>
	</target>
	<target name="main" depends="condition">
	</target>
</project>


参考:

http://blog.csdn.net/sodino/article/details/16923615

http://blog.163.com/ai_zxc/blog/static/462127201210201310593/

http://blog.sina.com.cn/s/blog_5eb1a2670100iwa2.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值