ANT – Using If Else arguments


Ant – If Else Arguments



”;


Ant allows to run targets based on passed conditions. We can use if statement or unless statement.

Syntax


<target name="copy" if="copyFile">
   <echo>Files are copied.</echo>
</target>
<target name="move" unless="copyFile">
   <echo>Files are moved.</echo>
</target>

We”ll be using -Dproperty to pass varible like copyFile to the build task. The variable is to be defined, the value of variable is of no relevance here.

Example

Create build.xml with the following content −


<?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>

Output

Running Ant on the above build file produces the following output −


F:tutorialspointant>ant -DcopyFile=true
Buildfile: F:tutorialspointantbuild.xml

copy:
   [echo] Files are copied.

BUILD SUCCESSFUL
Total time: 0 seconds

F:tutorialspointant>ant move
Buildfile: F:tutorialspointantbuild.xml

move:
   [echo] Files are moved.

BUILD SUCCESSFUL
Total time: 0 seconds

F:tutorialspointant>ant move -DcopyFile=true
Buildfile: F:tutorialspointantbuild.xml

move:

BUILD SUCCESSFUL
Total time: 0 seconds

F:tutorialspointant>ant move -DcopyFile=false
Buildfile: F:tutorialspointantbuild.xml

move:

BUILD SUCCESSFUL
Total time: 0 seconds

F:tutorialspointant>ant move -DcopyFile=true
Buildfile: F:tutorialspointantbuild.xml

move:

BUILD SUCCESSFUL
Total time: 0 seconds

F:tutorialspointant>ant move
Buildfile: F:tutorialspointantbuild.xml

move:
   [echo] Files are moved.

BUILD SUCCESSFUL
Total time: 0 seconds

F:tutorialspointant>

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *