
- Apache ANT Tutorial
- ANT - Home
- ANT - Introduction
- ANT - Environment Setup
- 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
- ANT - Executing Java code
- ANT - Eclipse Integration
- ANT - JUnit Integration
- ANT - Extending Ant
- 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 Resources
- ANT - Quick Guide
- ANT - Useful Resources
- ANT - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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:\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>
Advertisements