问题 为什么Ant taskdef无法在./net之外加载资源


当使用taskdef声明外部ant任务时,例如ant-contrib,建议的设置是使用followin taskdef:

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

当antcontrib.properties位于net / sf / antcontrib相对于build.xml文件时,这种方法有效。

但是当我把它放在lib / net / sf / antcontrib中并将taskdef更改为

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

Ant无法找到属性文件,它会给出错误

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

似乎ant分别处理lib目录并且无法从那里加载taskdef资源。


8693
2018-01-12 15:28


起源



答案:


正如亚历克斯所说,你不应该解开罐子。该 <taskdef> 可以直接从jar中加载antcontrib.properties。

您得到的错误是因为您更改了资源路径,但压缩jar / zip中文件的路径仍然相同。 taskdef没有注意你移动的属性文件,因为 <classpath> 你提供给 <taskdef> 告诉它只看罐子里。


5
2018-04-01 07:01



我有一个有效的<taskdef>,并在阅读了你的解释后将antcontrib.properties解压缩到./net/sf/antcontrib/,我意识到我可以删除属性文件,ant-contrib任务仍然有效。我使用了安装页面中提到的taskdef ant-contrib.sourceforge.net/#install - Ernelli
你救了我们几个小时的猜测 - 相对于jar路径是关键.. - Michael Pliskin


答案:


正如亚历克斯所说,你不应该解开罐子。该 <taskdef> 可以直接从jar中加载antcontrib.properties。

您得到的错误是因为您更改了资源路径,但压缩jar / zip中文件的路径仍然相同。 taskdef没有注意你移动的属性文件,因为 <classpath> 你提供给 <taskdef> 告诉它只看罐子里。


5
2018-04-01 07:01



我有一个有效的<taskdef>,并在阅读了你的解释后将antcontrib.properties解压缩到./net/sf/antcontrib/,我意识到我可以删除属性文件,ant-contrib任务仍然有效。我使用了安装页面中提到的taskdef ant-contrib.sourceforge.net/#install - Ernelli
你救了我们几个小时的猜测 - 相对于jar路径是关键.. - Michael Pliskin


使用 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任务一起使用,则attribute是可选的。


4
2018-01-13 00:26





要处理任务定义的类路径,我在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>

2
2018-02-09 08:07



好的提示;)但是你使用 <pathelement location="${bin}" /> 但是没有属性名称 bin。也许错误是符合的 <property name="classes" value="bin/" /> 哪里 classes 应该被替换 bin ...干杯;-) - olibre