msd_2
msd_2

Reputation: 1187

Using Ant condition tag

I want to do something like this

<target name="init" description="Create build directories.
    >
    <mkdir dir="bin" />
    <mkdir dir="dist" />
            <!-- Check for the presence of dependent JARs -->
            <condition property="dependent.files.available">
               <and>
                 <available file="../../xx.jar" />
                 <available file="../../yy.jar" />
               </and>
            </condition>
</target>

<!--- Compile -->
<target name="compile" depends="init" description="Compile java sources 
    and create classes">
         <if>
           <isset property="dependent.files.available"/>
             <then>
       <javac srcdir="src" destdir="bin" classpathref="ext_jar_classpath"/>
             </then>
             <else>
                <echo message="Either xx.jar or yy.jar not found"/>
             </else>
         </if>  
</target>

When I tried to compile the code it gave me the following error

Problem: failed to create task or type if
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any <presetdef>/<macrodef> declarations have taken place

It it the correct way of doing it?

Upvotes: 1

Views: 718

Answers (2)

mou
mou

Reputation: 343

Just a suggestion. You can do the same thing with ant 1.9.1 onwards using if/unless without ant-contrib (http://ant.apache.org/manual/targets.html)

Your targets would be

<target name="init" description="Create build directories.
    >
    <mkdir dir="bin" />
    <mkdir dir="dist" />
            <!-- Check for the presence of dependent JARs -->
            <condition property="dependent.files.available">
               <and>
                 <available file="../../xx.jar" />
                 <available file="../../yy.jar" />
               </and>
            </condition>
</target>

<!--- Compile -->
<target name="compile" depends="init" description="Compile java sources 
    and create classes" if="dependent.files.available">

       <javac srcdir="src" destdir="bin" classpathref="ext_jar_classpath"/>

</target>

Upvotes: 0

namero999
namero999

Reputation: 3002

You need to have the ant-contrib jar visible at runtime, or configure it correctly as described in the link.

The thing boils down to have Ant load the task definition, so if you put ant-contrib in ant/lib, you just need

<taskdef resource="net/sf/antcontrib/antcontrib.properties"/>

Upvotes: 3

Related Questions