好久不写ant脚本了,最近两天在用ant做web应用的安装部署脚本,为了实现web服务器的多版本兼容,必然要使用逻辑判断,比如我要判断是安装在weblogic8上还是weblogic9上,而在ant中处理逻辑判断真是麻烦,只能作用于task,要利用property来做判断,使用available来设置property。例如:
<?xml version="1.0" encoding="GB2312"?>
<project name="weblogic ant task" default="build">
<target name="detect.file" >
<condition property="fileIsExists" >
<and>
<available file="c:/123.txt"/>
</and>
</condition>
</target>
<target name="echoDemo" if="fileIsExists" depends="detect.file">
<echo message="hello ant"/>
</target>
<target name="build">
<antcall target="echoDemo"/>
</target>
</project>
上面判断一个文件,如果存在的话 fileIsExists 就为true,echoDemo这个task在执行前会先判断fileIsExists 是否为true如果不为true就不执行了。c盘下面有123.txt的话会打印hello ant 否则不会打印。
这里面还有一个小陷阱,我习惯使用antcall,不喜欢使用depends,但是使用antcall的话就会有问题,例如我最开始这么写的,就不行。
<?xml version="1.0" encoding="GB2312"?>
<project name="weblogic ant task" default="build">
<target name="detect.file">
<condition property="fileIsExists">
<and>
<available file="c:/123.txt"/>
</and>
</condition>
</target>
<target name="echoDemo" if="fileIsExists">
<echo message="hello ant"/>
</target>
<target name="build">
<antcall target="detect.file"/>
<antcall target="echoDemo"/>
</target>
</project>
使用antcall的话在echoDemo这个task执行的时候fileIsExists这个属性永远不为true,即便在执行完detect.file后它已经为true了,但是它不会被传递到下一个task,没用深入研究过ant,所以具体内部实现还不了解。
下面是ant的官方参考文档
更复杂的可以参考
http://ant.apache.org/manual/CoreTasks/conditions.html
Description
Sets a property if a certain condition holds true - this is a generalization of Available and Uptodate.
If the condition holds true, the property value is set to true by default; otherwise, the property is not set. You can set the value to something other than the default by specifying the value
attribute.
Conditions are specified as nested elements, you must specify exactly one condition.
Parameters
Attribute |
Description |
Required |
property |
The name of the property to set. |
Yes |
value |
The value to set the property to. Defaults to "true". |
No |
else |
The value to set the property to if the condition evaluates to false. By default the property will remain unset. Since Ant 1.6.3 |
No |
All conditions to test are specified as nested elements, for a complete list see here.
Examples
<condition property="javamail.complete"> <and> <available classname="javax.activation.DataHandler"/> <available classname="javax.mail.Transport"/> </and> </condition>
sets the property javamail.complete
if both the JavaBeans Activation Framework and JavaMail are available in the classpath.
<condition property="isMacOsButNotMacOsX"> <and> <os family="mac"/> <not> <os family="unix"/> </not> </and> </condition>
sets the property isMacOsButNotMacOsX
if the current operating system is MacOS, but not MacOS X - which Ant considers to be in the Unix family as well.
<condition property="isSunOSonSparc"> <os name="SunOS" arch="sparc"/> </condition>
sets the property isSunOSonSparc
if the current operating system is SunOS and if it is running on a sparc architecture.
分享到:
相关推荐
org.apache.tools.ant.Main org.apache.tools.ant.Task org.apache.tools.bzip2.CRC org.apache.tools.ant.Target org.apache.tools.ant.Project org.apache.tools.zip.ZipFile org.apache.tools.zip.ZipLong ...
import org.apache.tools.*; import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipFile; 等
apache-ant-1.10.12-bin.tar.gz
ant-contrib-1.0b2.jar
Ant_的最完整build.xml解释,Ant入门与进阶,ant学习工具书
最新版linux apache-ant-1.9.15-bin.tar.gz
标签:ant-contrib-r170.jar.zip,ant,contrib,r170,jar.zip包下载,依赖包
ant-contrib-1.0b3.jar ,ant循环打包需要的第三方jar包,可以实现批量循环打包
标签:ant-contrib-1.0b2.jar.zip,ant,contrib,1.0b2,jar.zip包下载,依赖包
标签:ant-props-1.0Alpha.jar.zip,ant,props,1.0Alpha,jar.zip包下载,依赖包
标签:ant-contrib-1.0b1.jar.zip,ant,contrib,1.0b1,jar.zip包下载,依赖包
标签:ant-junit4.jar.zip,ant,junit4,jar.zip包下载,依赖包
apache-ant-1.9.9-bin.tar.gz 【适用于linux系统,如CentOS、Ubuntu等】。
配置ant中的builder.xml文件。
apache-ant-1.10.12-bin.tar.bz2
Ant-contrib 为 Ant 提供了与通常所使用的编程语言功能相同的 <if> 、 、等逻辑判断任务,支持对字符串的排序任务,甚至还支持常见的数学运算,如加、减、乘、除、求余等功能。在构建过程中灵活运用这两个扩展包,将...
apache-ant-zip-2.3.jar maven仓库中不存在的jar,可以手动打包到项目中使用
apache-ant-1.9.16-bin.zip
ant.jar,ant-junit.jar,ant-launcher.jar,ant-trax.jar
Apache Ant,是一个将软件编译、测试、部署等步骤联系在一起加以自动化的一个工具,大多用于Java环境中的软件开发。由Apache软件基金会所提供。