Thanasis Pap
Thanasis Pap

Reputation: 2051

If isset in apache ant is failing

I am trying to check if a property exists in ant with the following:

<target name="test">
    <property name="testproperty" value="1234567890"/>
    <if>
        <isset property="testproperty"/>
        <then>
            <echo message="testproperty exists"/>
        </then>
        <else>
            <echo message="testproperty does not exist"/>
        </else>
    </if>
</target>

The result is a failure with the message:

build.xml:536: 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.

I must be doing something wrong with isset as the following runs smoothly:

<target name="test">
    <property name="testproperty" value="1234567890"/>
    <echo message="'${testproperty}'"/>
</target>

Please advice :)

Upvotes: 3

Views: 15435

Answers (3)

David W.
David W.

Reputation: 107040

You've found the missing jar, now you need to define the tasks that use that jar. I would put the jar under the antlib/antcontrib directory inside your project. This way, others who download your project will have the needed jar.

<taskdef resource="net/sf/antcontrib/antlib.xml">
    <classpath>
        <fileset dir="${basedir}/antlib/antcontrib"/>
    </classpath>
</taskdef>

If you use a lot of optional jar files, you might want to use namespaces:

 <project name="..." basedir="."  default="...."
    xmlns:ac="antlib://net/sf/antcontrib">

    <taskdef resource="net/sf/antcontrib/antlib.xml"
         uri="antlib://net/sf/antcontrib">
        <classpath>
            <fileset dir="${basedir}/antlib/antcontrib"/>
        </classpath>
    </taskdef>

Now, when you use a antcontrib task, you have to preface it with ac:. This allows optional jars to be used without namespace collisions.

<target name="test">
    <property name="testproperty" value="1234567890"/>
    <ac:if>
        <isset property="testproperty"/>
        <then>
            <echo message="testproperty exists"/>
        </then>
        <else>
            <echo message="testproperty does not exist"/>
        </else>
    </ac:if>
</target>

Upvotes: 3

Mark O&#39;Connor
Mark O&#39;Connor

Reputation: 77961

The if task is not a standard ANT task. This is how conditional execution of a target works

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

  <target name="property-exists" if="testproperty">
    <echo message="testproperty exists"/>
  </target>

  <target name="property-no-exists" unless="testproperty">
    <echo message="testproperty does not exist"/>
  </target>

  <target name="test" depends="property-exists,property-no-exists">
  </target>

</project>

Upvotes: 9

Thanasis Pap
Thanasis Pap

Reputation: 2051

Apparently I was missing the ant-contrib jar file that can be found here:
http://ant-contrib.sourceforge.net/

Upvotes: 7

Related Questions