【问题标题】:Resolving classpath with ant's taskdef and running only required target使用 ant 的 taskdef 解析类路径并仅运行所需的目标
【发布时间】:2023-03-13 00:50:01
【问题描述】:

我的 main-build.xml 看起来像:

<path id="run.classpath">
        <pathelement location="${build.lib.dir}/ant-{version}.jar"/>
        <pathelement location="${third-party.lib.dir}/some-{my-ver}.jar"/>
</path>

deploy.xml 在第三方 jar 中存在的类之一上使用 taskdef ant 任务:

<taskdef name="run-third-party-exec" classname="package.name.ThirdPartyExec"/>

sub-build.xml 导入 main-build.xmldeploy.xml 并尝试运行 deploy.xml 中存在的 ant 目标,但抱怨失败

taskdef package.name.ThirdPartyExec cannot be found

如何解决此类问题。由于在执行目标之前导入文件时,所有任务定义和导入都会执行,因此它失败了。不确定将所有抱怨的第三方 jar 添加到 ant 的类路径是否正确?

【问题讨论】:

    标签: ant classpath taskdef


    【解决方案1】:

    您的&lt;taskdef&gt; 需要知道在哪里可以找到package.name.ThirdPartyExec。通过提供类路径来做到这一点:

    <taskdef 
        name="run-third-party-exec" 
        classname="package.name.ThirdPartyExec" 
        classpathref="run.classpath" 
    />
    

    【讨论】:

    • 第三方 jar(s) 被添加到 main-build.xml 中的 ant 类路径中,因此当调用 taskdef 时,deploy.xml 中没有提到 classpathref。并且从 sub-build.xml 调用 deploy.xml 的目标失败,因为它在 ant 的类路径中找不到所需的 jar
    • 就其本身而言,main-build.xml 中的 &lt;path id="run.classpath"&gt; 不会向任何类路径添加任何内容。 Ant 启动后,无法添加到“Ant 的类路径”中。单个任务可以有自己的类路径。这就是classpathref 提供的——一种让任务拥有自己的类路径的方法。您的脚本需要结构化,以便首先运行&lt;path id="run.classpath"&gt;,然后可以将run.classpath 引用传递给需要它的任务。请参阅the Optional Tasks section of Installing Apache Ant 了解更多背景信息。
    猜你喜欢
    • 2019-05-11
    • 2011-04-25
    • 1970-01-01
    • 2011-05-29
    • 2010-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-14
    相关资源
    最近更新 更多