3

Can someone please explain me with Example of Ant condition property with "if", "and", "not" and "istrue" with the code??

I'm new to Ant and need help on this.

Basically i need to download file using FTP. I have code to download it. but there are few condition i need to check before downloading.

  1. Before downloading check the Downloadstatusvariable value (usually its true or false value")
  2. check whether file is already downloaded or available in the path
  3. if both the conditions fails then exec the FTP download code.

Thanks in advance.

Mark O'Connor
  • 76,015
  • 10
  • 139
  • 185
coolgokul
  • 257
  • 1
  • 7
  • 18

3 Answers3

12

ant-contrib is widely-used library and the if/else/for/foreach tasks will not only make it easier to write complex ant scripts, they'll make your ant scripts much more readable.

But, since you insist :)

The way I've handled this in the past is to set up a task A that only executes if property X is set, and make task A depend on some task B that checks the conditions and sets property X if they are true. For example:

<target name="get-ftp-file" depends="check-need-ftp-file" if="ftp.call.required">
  <ftp ... />
</target>

<target name="check-need-ftp-file">
  <condition property="ftp.call.required">
    <and>
      <not>
        <!-- Checks if Downloadstatusvariable variable is true -->
        <istrue value="${Downloadstatusvariable}"/>
      </not>
      <not>
        <!-- Checks if file is present in filesystem -->
        <available file="${my.file}"/>
      </not>
    </and>
  </condition>
</target>

EDIT:

coolgokul, the <and> and <not> tasks are standard logical operators. The <available> condition in the example above evaluates to true if the file exists on the local machine. But we want to download the file only if the file does NOT exist on the local machine, so we wrap it up in the <not> task. Similar for the ${Downloadstatusvariable} check--we want to make sure it is not true, so we use <istrue> and then wrap it up in <not>.

Finally, we want to make sure that that the variable is not true AND the file does not exist, so we wrap up both those conditions in <and>. Translated to English, we're asking "Is Downloadstatusvariable not true, and is the downloaded file not available?"

Recommend you read more about conditional operators @ http://ant.apache.org/manual/Tasks/conditions.html

Hope that helps.

choover
  • 852
  • 7
  • 12
  • Hey Choover, Thanks for your help. Can you please explain me wat "and" and "not" operator function over here. I always got confused with these things...:-( – coolgokul Jul 19 '12 at 14:49
  • Good answer. I have to disagree that ant-contrib is a good idea, precisely because it enables you to create complex build scripts.... but it's certainly a matter of taste. Personally I embed a scripting language (like groovy) when the standard tasks in ANT aren't up to the job. – Mark O'Connor Jul 19 '12 at 18:21
  • Mark O'Connor, I'd never seen embedded groovy in an Ant script before. Thanks for the tip! – choover Jul 19 '12 at 23:27
  • 1
    this is an excellent example as it worked for my use-case as well (with one little change), but I think you need to change the "task" XML element to "target"? – bcmoney May 22 '13 at 20:30
1

The standard Ant does not have an "if" task, but you can do pretty much any conditional logic you need to by using the "condition' task. See http://ant.apache.org/manual/Tasks/condition.html for some examples.

There is a set of additional tasks including an "if" task in the ant-contrib library, but I'd advise against using this unless you really need to, due to the complexity it will add to your build environment.

gareth_bowles
  • 20,760
  • 5
  • 52
  • 82
  • 1
    thanks Gareth. I dont want to use the Ant contrib lib. I need to use the Standard Ant. I read that document but feel like i need some more examples for Condition property with "and" and "not" and "iftrue". – coolgokul Jul 18 '12 at 19:05
0

Are you trying out using Ant Contrib Task?. If you click on the links over there, the document explains with example what you might want to achieve through each of the tasks

Some relevant examples might be here: Steve J's Blog

Sujay
  • 6,753
  • 2
  • 30
  • 49
  • Thanks Sujay. I dont want to use the Ant Contrib Task. I just need to use the Standard Ant. – coolgokul Jul 18 '12 at 19:03
  • perhaps you can tell us what you exactly want to achieve? that way, it'll be easier for us to help you out with some pointers. I don't think stackoverflow is going to become a tutorial site in the very near future! :) perhaps you might want to modify your question and add some more information to it. – Sujay Jul 18 '12 at 19:15
  • Thanks Sujay.. Will edit and the question and post it again. :-) – coolgokul Jul 18 '12 at 19:23