【问题标题】:Why cannot Ant taskdef cannot load a resource outside ./net为什么 Ant taskdef 不能在 ./net 之外加载资源
【发布时间】:2011-01-04 05:40:27
【问题描述】:

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

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

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

但是当我把它放在 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资源。

【问题讨论】:

    标签: ant taskdef


    【解决方案1】:

    正如 Alex 所说,您不需要解压缩 jar。 &lt;taskdef&gt; 可以直接从 jar 中加载 antcontrib.properties。

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

    【讨论】:

    • 我有一个有效的 并且在阅读您的解释后也将 antcontrib.properties 提取到 ./net/sf/antcontrib/ 中,我意识到我可以删除属性文件和 ant-贡献任务仍然有效。我使用了安装页面ant-contrib.sourceforge.net/#install中提到的taskdef
    【解决方案2】:

    使用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 文件中提取任何内容。此外,如果您不想将命名空间用于 antcontrib 任务,uri 属性是可选的。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • 好提示 ;) 但是您使用 &lt;pathelement location="${bin}" /&gt; 但没有属性名称 bin。也许错误在&lt;property name="classes" value="bin/" /&gt; 中,其中classes 应该替换为bin ...干杯;-)
      猜你喜欢
      • 2017-08-18
      • 1970-01-01
      • 2021-07-29
      • 1970-01-01
      • 2011-08-17
      • 1970-01-01
      • 2021-05-01
      • 2013-09-04
      • 1970-01-01
      相关资源
      最近更新 更多