1.根据特定条件执行目标
设置目标if和/或unless属性为某个特征名称完成的。
<?xml version="1.0" encoding="UTF-8"?> <project name="property" default="echoall"> <target name="echoall" if="output"> <echo message="echoall:执行echoall,output is ${output}"/> </target> </project>
输出,在特征output在定义时输出内容,未定义时不输出内容,与特征的值无关:
e:\antspace\property>ant Buildfile: e:\antspace\property\build.xml echoall: BUILD SUCCESSFUL Total time: 0 seconds e:\antspace\property>ant -Doutput=a Buildfile: e:\antspace\property\build.xml echoall: [echo] echoall:执行echoall,output is a BUILD SUCCESSFUL Total time: 0 seconds
2. 设定构建失败条件
测试:
<?xml version="1.0" encoding="UTF-8"?> <project name="property" default="echoall"> <target name="echoall"> <echo message="echoall:执行echoall,output is ${output}"/> </target> <fail message="output未定义,构建结束" unless="output"> </fail> </project>
或
<?xml version="1.0" encoding="UTF-8"?> <project name="property" default="echoall"> <target name="echoall"> <echo message="echoall:执行echoall,output is ${output}"/> <fail message="output未定义,构建结束" unless="output" /> </target> </project>
输出:
e:\antspace\property>ant Buildfile: e:\antspace\property\build.xml BUILD FAILED e:\antspace\property\build.xml:8: output未定义,构建结束 Total time: 0 seconds e:\antspace\property>ant -Doutput=a Buildfile: e:\antspace\property\build.xml echoall: [echo] echoall:执行echoall,output is a BUILD SUCCESSFUL Total time: 0 seconds
3.根据特定条件包含/排除模式集
测试:
<?xml version="1.0" encoding="UTF-8"?> <project name="property" default="buildjava"> <target name="buildjava" depends="mkbuild"> <javac srcdir="src" includeantruntime="false" destdir="build"> <exclude name="xuj/ant/Main.java" unless="build"/> </javac> </target> <target name="mkbuild"> <delete dir="build"/> <mkdir dir="build"/> </target> </project>
输出:
e:\antspace\property>ant Buildfile: e:\antspace\property\build.xml mkbuild: [delete] Deleting directory e:\antspace\property\build [mkdir] Created dir: e:\antspace\property\build buildjava: BUILD SUCCESSFUL Total time: 0 seconds e:\antspace\property>ant -Dbuild=a Buildfile: e:\antspace\property\build.xml mkbuild: [delete] Deleting directory e:\antspace\property\build [mkdir] Created dir: e:\antspace\property\build buildjava: [javac] Compiling 1 source file to e:\antspace\property\build BUILD SUCCESSFUL Total time: 0 seconds