Ant - If Else 参数

  • 简述

    Ant 允许根据传递的条件运行目标。我们可以用if声明或unless陈述。
  • 句法

    
    
    <target name="copy" if="copyFile">
    
       <echo>Files are copied.</echo>
    
    </target>
    
    <target name="move" unless="copyFile">
    
       <echo>Files are moved.</echo>
    
    </target>
    
    
    我们将使用 -Dproperty 将像 copyFile 这样的变量传递给构建任务。变量是要定义的,变量的值在这里无关紧要。
  • 例子

    使用以下内容创建 build.xml -
    
    
    <?xml version="1.0"?>
    
    <project name="sample" basedir="." default="copy">
    
       <target name="copy" if="copyFile">
    
          <echo>Files are copied.</echo>
    
       </target>
    
       <target name="move" unless="copyFile">
    
          <echo>Files are moved.</echo>
    
       </target>
    
    </project>
    
    
  • 输出

    在上述构建文件上运行 Ant 会产生以下输出 -
    
    
    F:\jc2182\ant>ant -DcopyFile=true
    
    Buildfile: F:\jc2182\ant\build.xml
    
    
    
    copy:
    
       [echo] Files are copied.
    
    
    
    BUILD SUCCESSFUL
    
    Total time: 0 seconds
    
    
    
    F:\jc2182\ant>ant move
    
    Buildfile: F:\jc2182\ant\build.xml
    
    
    
    move:
    
       [echo] Files are moved.
    
    
    
    BUILD SUCCESSFUL
    
    Total time: 0 seconds
    
    
    
    F:\jc2182\ant>ant move -DcopyFile=true
    
    Buildfile: F:\jc2182\ant\build.xml
    
    
    
    move:
    
    
    
    BUILD SUCCESSFUL
    
    Total time: 0 seconds
    
    
    
    F:\jc2182\ant>ant move -DcopyFile=false
    
    Buildfile: F:\jc2182\ant\build.xml
    
    
    
    move:
    
    
    
    BUILD SUCCESSFUL
    
    Total time: 0 seconds
    
    
    
    F:\jc2182\ant>ant move -DcopyFile=true
    
    Buildfile: F:\jc2182\ant\build.xml
    
    
    
    move:
    
    
    
    BUILD SUCCESSFUL
    
    Total time: 0 seconds
    
    
    
    F:\jc2182\ant>ant move
    
    Buildfile: F:\jc2182\ant\build.xml
    
    
    
    move:
    
       [echo] Files are moved.
    
    
    
    BUILD SUCCESSFUL
    
    Total time: 0 seconds
    
    
    
    F:\jc2182\ant>