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>

我们将使用 -Property 将复制文件等变量传递给构建任务。 变量是要定义的,变量的值在这里无关紧要。


示例

使用以下内容创建 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:\tutorialspoint\ant>ant -DcopyFile=true
Buildfile: F:\tutorialspoint\ant\build.xml

copy:
   [echo] Files are copied.

BUILD SUCCESSFUL
Total time: 0 seconds

F:\tutorialspoint\ant>ant move
Buildfile: F:\tutorialspoint\ant\build.xml

move:
   [echo] Files are moved.

BUILD SUCCESSFUL
Total time: 0 seconds

F:\tutorialspoint\ant>ant move -DcopyFile=true
Buildfile: F:\tutorialspoint\ant\build.xml

move:

BUILD SUCCESSFUL
Total time: 0 seconds

F:\tutorialspoint\ant>ant move -DcopyFile=false
Buildfile: F:\tutorialspoint\ant\build.xml

move:

BUILD SUCCESSFUL
Total time: 0 seconds

F:\tutorialspoint\ant>ant move -DcopyFile=true
Buildfile: F:\tutorialspoint\ant\build.xml

move:

BUILD SUCCESSFUL
Total time: 0 seconds

F:\tutorialspoint\ant>ant move
Buildfile: F:\tutorialspoint\ant\build.xml

move:
   [echo] Files are moved.

BUILD SUCCESSFUL
Total time: 0 seconds

F:\tutorialspoint\ant>