
Apache ANT Basics
- ANT - Build Files
- ANT - Property Task
- ANT - Property Files
- ANT - Data Types
- ANT - Building Projects
- ANT - Build Documentation
- ANT - Creating JAR files
- ANT - Create WAR Files
- ANT - Packaging Applications
- ANT - Deploying Applications
Apache ANT Advanced
Apache ANT Useful Examples
- ANT - Using Token
- ANT - Using Command Line Arguments
- ANT - Using If Else arguments
- ANT - Custom Components
- ANT - Listeners and Loggers
Apache ANT Useful Resources
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 −
Example - copyFile passed as true
F:\tutorialspoint\ant>ant -DcopyFile=true Buildfile: F:\tutorialspoint\ant\build.xml copy: [echo] Files are copied. BUILD SUCCESSFUL Total time: 0 seconds
Example - copyFile not passed
F:\tutorialspoint\ant>ant move Buildfile: F:\tutorialspoint\ant\build.xml move: [echo] Files are moved. BUILD SUCCESSFUL Total time: 0 seconds
Example - copyFile passed as true
F:\tutorialspoint\ant>ant move -DcopyFile=true Buildfile: F:\tutorialspoint\ant\build.xml move: BUILD SUCCESSFUL Total time: 0 seconds
Example - copyFile passed as false
F:\tutorialspoint\ant>ant move -DcopyFile=false Buildfile: F:\tutorialspoint\ant\build.xml move: BUILD SUCCESSFUL Total time: 0 seconds
Advertisements