Taskdef + available task leads to failure when using from Gradle

505 views Asked by At

I'm loading ant-contrib through a taskdef Then I'm checking with the available task that the 'IfTask' is created. This fails

<taskdef resource="net/sf/antcontrib/antcontrib.properties" classpath="/path/to/ant/contrib.jar"/>

<condition property="IfTaskAvailable">
<available classname="net.sf.antcontrib.logic.IfTask"/>
</condition>
<fail message="The If task is not defined" unless="IfTaskAvailable"/>

The available task does not seem to check using the same class loader used by the taskdef ?

EDIT after Oleg Pavliv answer After a bit of gidding this effectively works. I should have been clearer in my question.

This works in Ant, but this does not work when calling ant from Gradle, i.e. in a gradle build:

ant.taskdef(uri:'antlib:net.sf.antcontrib', resource:'net/sf/antcontrib/antcontrib.properties', classpath: '/path/to/ant/contrib.jar')
ant.available(classname:'net.sf.antcontrib.logic.IfTask')
1

There are 1 answers

2
Oleg Pavliv On

Probably the /path/to/ant/contrib is not correct

On my computer it works

<project  xmlns:if="ant:if"  xmlns:unless="ant:unless">

    <taskdef resource="net/sf/antcontrib/antcontrib.properties" classpath="c:/Soft/Java/apache-ant-1.9.4/lib/ant-contrib-1.0b3.jar"/>

    <condition property="IfTaskAvailable">
        <available classname="net.sf.antcontrib.logic.IfTask"/>
    </condition>

    <fail message="Error: The If task is not defined" unless="IfTaskAvailable"/>
    <echo message="IfTaskAvailable: ${IfTaskAvailable}" />

</project>

The output

/cygdrive/c/temp/ant>ant
Buildfile: c:\temp\ant\build.xml
     [echo] IfTaskAvailable: true

BUILD SUCCESSFUL
Total time: 0 seconds

EDIT: If you call it from gradle you should specify the classpath in available as well.

<project  xmlns:if="ant:if"  xmlns:unless="ant:unless">

    <target name="main">
        <taskdef resource="net/sf/antcontrib/antcontrib.properties" classpath="c:/Soft/Java/apache-ant-1.9.4/lib/ant-contrib-1.0b3.jar"/>

        <condition property="IfTaskAvailable">
            <available classname="net.sf.antcontrib.logic.IfTask" classpath="c:/Soft/Java/apache-ant-1.9.4/lib/ant-contrib-1.0b3.jar"/>
        </condition>

        <fail message="Error: The If task is not defined" unless="IfTaskAvailable"/>
        <echo message="IfTaskAvailable: ${IfTaskAvailable}" />
    </target>
</project>