Search code examples
javaxmlantjarant-contrib

available task in ant


Can i set an variable in available tag like this?

<available file="${inf.root}/schema_params/@{componame}-schema.sql" 
   type="file" 
   variable="schema.file" />

because when i use property instead of variable in available tag, Its value is immutable. But i want to change it at run time.Below is my code.i want to copy 1 file checking through my component list. if the file exists, i have to copy and move it. else i have to skip the logic. But whats happening is, if i dont use this code,

<var name="schema.params.file" unset="true"/>
<property name="schema.params.file" value="false"/>
<var name="scripts.dir" unset="true"/>
<property name="scripts.dir" value="false"/>

in the first iteration if schema.params.file,scripts.dir is set to true if files exist, it is not overided in the next iteration even though if file doenot exist. so i have added above code.but now always the values are set to false again by above code. how can i overcome the issue of overiding these 2 schema.params.file,scripts.dir in every iteration?

<for list="${t1.@{componentname}}" param="installableid" delimiter="${line.separator}">
    <sequential>
        <available file="${infinys.root}/schema_params/@{componentname}-schema_params.sql" 
             type="file" 
             property="schema.params.file" />
        <available file="${stage.dir}/@{componentname}/@{installableid}/schema/install/scripts" 
             type="dir" property="scripts.dir"/>
        <if>
            <and>
                <equals arg1="true" arg2="${schema.params.file}" />
                <equals arg1="true" arg2="${scripts.dir}" />  
            </and>                      
            <then>
                <copy file="${infinys.root}/schema_params/@{componentname}-schema_params.sql"
                   todir="${stage.dir}/@{componentname}/@{installableid}/schema/install/scripts"
                   failonerror="false" />
                <move file="${stage.dir}/@{componentname}/@{installableid}/schema/install/scripts/@{componentname}-schema_params.sql" 
                      tofile="${stage.dir}/@{componentname}/@{installableid}/schema/install/scripts/schema_params.sql"
                      failonerror="false"/>
                <chmod file="${stage.dir}/@{componentname}/@{installableid}/schema/install/scripts/schema_params.sql" perm="775"/>
                <var name="schema.params.file" unset="true"/>
                <property name="schema.params.file" value="false"/>
                <var name="scripts.dir" unset="true"/>
                <property name="scripts.dir" value="false"/>  
            </then>
        </if>
    </sequential>
</for>

Solution

  • Can i set an variable in available tag like this?

    Yes, you can.

    Macro names are changed with each iteration. The <var/> task is simply a way to unset and reset a property in Ant. It's part of the Ant-Contrib project. You don't need to unset and reset the property:

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

    You could do this in a single statement:

    <var name="schema.params.file" value="false"/>
    

    Their use is highly discouraged since it breaks Ant's immutable property idea. However, I've find that I too use <var/> a lot when going through <for/> loops and sometimes <macrodef>. Newer versions of Ant allow you to localize properties, so I suspect the <var/> task will soon no longer be needed.

    Another thing which may make things a bit easier is that you can use <if/> tests with <available/>

    <if>
        <then>
            <and>
                <available file="${infinys.root}/schema_params/@{componentname}-schema_params.sql" 
                    type="file"/>
                <available file="${stage.dir}/@{componentname}/@{installableid}/schema/install/scripts" 
                    type="dir"/>
            </and>
            <sequencial>
                ....
            </sequential/>
        </then>
    </if>
    

    Doing this may make your code a bit cleaner and easier to understand. It will also eliminate the need to unset properties in the first place.