Available tag in ant is always true if a file is unavailable also

1.1k Views Asked by At

This code is always returning a true value even if file at given path does not exists

   <available file="${x}/schema/@{componentname}-schema.sql" type="file"      property="schema.file" />
      <if>
          <equals arg1="true" arg2="${schema.file}" />
            <then>
        <debug message="****schemafile is  ${schema.file} ******" />
            </then>
</if>

Output is always :- *schemafile is true***

even if file is not available at that path. Please help me to find the error.

2

There are 2 best solutions below

0
On BEST ANSWER

problem was i was iterating above code in for loop, and since property is immutable, it is always set to true if set at-least once. Thats why after 1 iteration even if the file was not found, it echoes schemafile is true** .

i have added below code to set property to false after that code

<var name="schema.file" unset="true"/>
<property name="schema.file" value="false"/>
3
On

I've refactored your example, in order to use standard ANT tasks:

<project name="demo" default="run" xmlns:if="ant:if">

  <property name="src.dir" location="src"/>

  <target name="run">
    <available file="${src.dir}/schema/schema.sql" type="file" property="schema.file" />
    <echo message="****schemafile is  ${schema.file} ******" if:set="schema.file"/>
  </target>

</project>

Notes:

  • I don't recognise the "debug" task so use the standard "echo" task instead
  • I recommend not using the ant-contrib "if" task. ANT 1.9.1 introduced an if attribute which can be used instead.

The following alternative variant will work with older versions of ANT. It uses an "if" target attribute to perform conditional execution:

<project name="demo" default="run">

  <property name="src.dir" location="src"/>
  <available file="${src.dir}/schema/schema.sql" type="file" property="schema.file" />

  <target name="run" if="schema.file">
    <echo message="****schemafile is  ${schema.file} ******"/>
  </target>

</project>