代码之家  ›  专栏  ›  技术社区  ›  Ernelli

为什么ant taskdef不能在外部加载资源?/net

  •  11
  • Ernelli  · 技术社区  · 15 年前

    当使用taskdef(例如ant contrib)声明外部Ant任务时,建议的设置是使用以下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资源。

    3 回复  |  直到 10 年前
        1
  •  5
  •   gmcnaughton    14 年前

    正如亚历克斯所说,你不应该把罐子拉开。这个 <taskdef> 无法直接从jar中加载antcontrib.properties。

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

        2
  •  4
  •   Tim Keating    10 年前

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

        3
  •  2
  •   SRG    15 年前

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