Ant如果有其他条件?

jon*_*ney 39 ant

是否有可以用于Ant任务的if/else条件?

这是我到目前为止所写的:

<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>
Run Code Online (Sandbox Code Playgroud)

如果条件为真,上面的脚本将复制该文件.如果条件为假并且我希望复制另一个文件怎么办?这可能在Ant?

我可以将一个参数传递给上面的任务,并确保传递的参数是

Mad*_*sen 39

if属性不存在<copy>.它应该适用于<target>.

下面是如何使用depends目标属性ifunless属性来控制从属目标执行的示例.两个中只有一个应该执行.

<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>
Run Code Online (Sandbox Code Playgroud)

如果您使用的是ANT 1.8+,则可以使用属性扩展,它将评估属性的值以确定布尔值.所以,你可以用if="${copy-condition}"而不是if="copy-condition".

在ANT 1.7.1和更早版本中,您指定属性的名称.如果属性已定义且具有任何值(即使是空字符串),则它将评估为true.


Dav*_*vid 21

如果任务,您也可以使用ant contrib's执行此操作.

<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>
Run Code Online (Sandbox Code Playgroud)

  • 仅仅因为它陈旧并不意味着它没有任何好处或者仍然没有工作:) (18认同)
  • 还应注意,不再维持_ant contrib_.最后一个版本是2008年! (6认同)

Mar*_*nor 14

使用目标上的条件的古怪语法(由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>
Run Code Online (Sandbox Code Playgroud)

ANT支持几种语言(参见脚本任务),我的偏好是Groovy,因为它的语法简洁,因为它在构建时运行良好.

道歉,大卫我不是ant-contrib的粉丝.

  • 不知道如何让它工作 - 似乎没有 groovy 标签。`未能创建任务或键入 groovy`。 (2认同)

小智 8

从ant 1.9.1开始,你可以使用if:set条件:https://ant.apache.org/manual/ifunless.html