是否有一个if / else条件,我可以用一个Ant任务?
这是我到目前为止写的:
<target name="prepare-copy" description="copy file based on condition">
<echo>Get file based on condition</echo>
<copy file="${some.dir}/true" todir="." if="true"/>
</target>
该脚本如果条件为真,上面会复制文件。 如果条件为假,我要复制另一个文件? 这有可能在蚂蚁?
我可以通过一个参数去上面的任务,并确定这是通过参数是
所述if
属性不存在对<copy>
。 应当施加到<target>
。
下面是你如何使用的例子depends
目标和属性if
和unless
属性来控制的依赖的目标执行。 只有两个中的一个应该执行。
<target name="prepare-copy" description="copy file based on condition"
depends="prepare-copy-true, prepare-copy-false">
</target>
<target name="prepare-copy-true" description="copy file based on condition"
if="copy-condition">
<echo>Get file based on condition being true</echo>
<copy file="${some.dir}/true" todir="." />
</target>
<target name="prepare-copy-false" description="copy file based on false condition"
unless="copy-condition">
<echo>Get file based on condition being false</echo>
<copy file="${some.dir}/false" todir="." />
</target>
如果您正在使用ANT 1.8+,那么你就可以使用属性扩展,它会评估属性的值来确定布尔值。 所以,你可以使用if="${copy-condition}"
,而不是if="copy-condition"
。
在ANT 1.7.1及更早版本,指定属性的名称。 如果定义了属性,并具有任何价值(甚至是空字符串),那么它的值为true。
你也可以用做蚂蚁contrib请的,如果任务 。
<if>
<equals arg1="${condition}" arg2="true"/>
<then>
<copy file="${some.dir}/file" todir="${another.dir}"/>
</then>
<elseif>
<equals arg1="${condition}" arg2="false"/>
<then>
<copy file="${some.dir}/differentFile" todir="${another.dir}"/>
</then>
</elseif>
<else>
<echo message="Condition was neither true nor false"/>
</else>
</if>
使用对目标(通过的Mads描述的)条件的古怪语法是执行在芯ANT条件执行的唯一支持方式。
ANT是不是一种编程语言和当事情变得复杂,我选择我的构建中嵌入的脚本如下:
<target name="prepare-copy" description="copy file based on condition">
<groovy>
if (properties["some.condition"] == "true") {
ant.copy(file:"${properties["some.dir"]}/true", todir:".")
}
</groovy>
</target>
ANT支持多种语言(见脚本任务),我喜欢的是Groovy的 ,因为它的简洁的语法因为它起着这么好与构建。
道歉,大卫·我不是一个球迷蚂蚁的contrib 。
由于蚂蚁1.9.1你可以使用一个如果:设定条件: https://ant.apache.org/manual/ifunless.html
<project name="Build" basedir="." default="clean">
<property name="default.build.type" value ="Release"/>
<target name="clean">
<echo>Value Buld is now ${PARAM_BUILD_TYPE} is set</echo>
<condition property="build.type" value="${PARAM_BUILD_TYPE}" else="${default.build.type}">
<isset property="PARAM_BUILD_TYPE"/>
</condition>
<echo>Value Buld is now ${PARAM_BUILD_TYPE} is set</echo>
<echo>Value Buld is now ${build.type} is set</echo>
</target>
</project>
在我的情况DPARAM_BUILD_TYPE=Debug
如果不是提供的,我需要建立用于调试,否则我需要去构建发布版本。 我写这样上面的条件,它的工作和下面它工作正常,我我已经测试。
和财产${build.type}
我们可以通过这个来,我在我的其他蚂蚁macrodef正在做的其他目标或macrodef进行处理。
D:\>ant -DPARAM_BUILD_TYPE=Debug
Buildfile: D:\build.xml
clean:
[echo] Value Buld is now Debug is set
[echo] Value Buld is now Debug is set
[echo] Value Buld is now Debug is set
main:
BUILD SUCCESSFUL
Total time: 0 seconds
D:\>ant
Buildfile: D:\build.xml
clean:
[echo] Value Buld is now ${PARAM_BUILD_TYPE} is set
[echo] Value Buld is now ${PARAM_BUILD_TYPE} is set
[echo] Value Buld is now Release is set
main:
BUILD SUCCESSFUL
Total time: 0 seconds
它为我工作落实情况如此张贴希望它会有所帮助。