Apache ANT Tutorial
Apache ANT Resources
Selected Reading
- ANT - Listeners and Loggers
- ANT - Custom Components
- ANT - Using If Else arguments
- ANT - Using Command Line Arguments
- ANT - Using Token
- ANT - Extending Ant
- ANT - JUnit Integration
- ANT - Eclipse Integration
- ANT - Executing Java code
- ANT - Deploying Applications
- ANT - Packaging Applications
- ANT - Create WAR Files
- ANT - Creating JAR files
- ANT - Build Documentation
- ANT - Building Projects
- ANT - Data Types
- ANT - Property Files
- ANT - Property Task
- ANT - Build Files
- ANT - Environment Setup
- ANT - Introduction
- ANT - Home
Apache ANT Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
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 pke 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: utorialspointant>ant -DcopyFile=true Buildfile: F: utorialspointantuild.xml copy: [echo] Files are copied. BUILD SUCCESSFUL Total time: 0 seconds F: utorialspointant>ant move Buildfile: F: utorialspointantuild.xml move: [echo] Files are moved. BUILD SUCCESSFUL Total time: 0 seconds F: utorialspointant>ant move -DcopyFile=true Buildfile: F: utorialspointantuild.xml move: BUILD SUCCESSFUL Total time: 0 seconds F: utorialspointant>ant move -DcopyFile=false Buildfile: F: utorialspointantuild.xml move: BUILD SUCCESSFUL Total time: 0 seconds F: utorialspointant>ant move -DcopyFile=true Buildfile: F: utorialspointantuild.xml move: BUILD SUCCESSFUL Total time: 0 seconds F: utorialspointant>ant move Buildfile: F: utorialspointantuild.xml move: [echo] Files are moved. BUILD SUCCESSFUL Total time: 0 seconds F: utorialspointant>Advertisements