11

When declaring external ant tasks using taskdef, for instance ant-contrib, the proposed setup is to use the followin taskdef:

<taskdef resource="net/sf/antcontrib/antcontrib.properties">
  <classpath>
    <pathelement location="lib/ant-contrib/ant-contrib-1.0b3.jar"/>
  </classpath>
</taskdef>

This works when antcontrib.properties is located in net/sf/antcontrib relative to the build.xml file.

But when I put it in lib/net/sf/antcontrib and changes the taskdef into

<taskdef resource="lib/net/sf/antcontrib/antcontrib.properties">
  <classpath>
    <pathelement location="lib/ant-contrib/ant-contrib-1.0b3.jar"/>
  </classpath>
</taskdef>

Ant is not able to find the properties file, it gives the error

[taskdef] Could not load definitions from resource
lib/net/sf/antcontrib/antcontrib.properties. It could not be found.

It seems like ant treats the lib directory separately and fails to load a taskdef resource from there.

Ernelli
  • 3,960
  • 3
  • 28
  • 34

3 Answers3

5

As Alex said, you shouldn't need to unzip the jar. The <taskdef> can load antcontrib.properties directly out of the jar.

The error you got is because you changed the resource path, but the path to the file inside the compressed jar/zip is still the same. The taskdef isn't paying attention to the properties file you moved because the <classpath> you provided to <taskdef> tells it to only look in the jar.

gmcnaughton
  • 2,233
  • 1
  • 21
  • 28
  • 1
    I had a valid and had also extracted the antcontrib.properties into ./net/sf/antcontrib/ after reading your explanation, I realized that I could remove the properties file and the ant-contrib tasks still worked. I used the the taskdef mentioned in the installation page http://ant-contrib.sourceforge.net/#install – Ernelli Apr 06 '10 at 08:17
4

Use antlib.xml resource:

Here is the taskdef definition that I use:

<property name="ant-contrib.jar" location="..."/>

<taskdef
  resource="net/sf/antcontrib/antlib.xml"
  uri="http://ant-contrib.sourceforge.net"
>
  <classpath>
    <pathelement location="${ant-contrib.jar}"/>
  </classpath>
</taskdef>

You do not need to extract anything from the jar file. Also, uri attribute is optional if you do not want to use namespaces with antcontrib tasks.

Tim Keating
  • 6,443
  • 4
  • 47
  • 53
Alexander Pogrebnyak
  • 44,836
  • 10
  • 105
  • 121
2

To handle classpath for tasks definitions, I use a classpath ref in Ant, it's way easier. You can link either a directory containing classes, either a directory containing many .jar, either (of course) a single .jar.

For example :

    <!-- Properties -->
    <property name="lib" value="lib/" />
    <property name="classes" value="bin/" />

    <!-- Classpath definition -->
    <path id="runtime-classpath" >
        <pathelement location="${bin}" />
        <fileset dir="${lib}">
            <include name="*.jar"/>
        </fileset>
    </path>

    <!-- Taskdefs definitions -->
    <taskdef name="myTask" classname="org.stackoverflow.tasks.MyTask" classpathref="runtime-classpath" />

    <!-- Tasks -->
    <target name="test" description="Test Action">
            <myTask parameter1="value" />
    </target>
SRG
  • 1,569
  • 1
  • 17
  • 19
  • Good tip ;) However you use `` but there is no property name `bin`. Maybe the mistake is in line `` where `classes` should be replaced by `bin` ... Cheers ;-) – oHo Jul 24 '13 at 15:19