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.

4

3 回答 3

5

正如亚历克斯所说,您不需要解压缩罐子。可以直接从 jar中<taskdef>加载 antcontrib.properties。

你得到的错误是因为你改变了资源路径,但是压缩的jar/zip里面的文件的路径还是一样的。taskdef 没有注意您移动的属性文件,因为<classpath>您提供的<taskdef>内容告诉它只查看 jar。

于 2010-04-01T07:01:50.697 回答
4

使用antlib.xml资源:

这是我使用的 taskdef 定义:

<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>

您不需要从 jar 文件中提取任何内容。此外,uri如果您不想将命名空间用于 antcontrib 任务,则属性是可选的。

于 2010-01-13T00:26:18.867 回答
2

为了处理任务定义的类路径,我在 Ant 中使用了一个类路径引用,这更容易。您可以链接包含类的目录,或者包含许多 .jar 的目录,或者(当然)单个 .jar。

例如 :

    <!-- 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>
于 2010-02-09T08:07:20.777 回答