Ant:如何测试一个目标是否存在(如果它不存在则不称之为';t)?

Ant:如何测试一个目标是否存在(如果它不存在则不称之为';t)?,ant,conditional,target,antcall,Ant,Conditional,Target,Antcall,我有一组构建文件,其中一些调用其他文件——首先导入它们。最终版本可能有或没有特定的目标(例如“copyother”)。如果目标是在行尾构建脚本中定义的,我想从主构建文件调用它。我怎么做 调用脚本的一部分: <!-- Import project-specific libraries and classpath --> <property name="build.dir" value="${projectDir}/build"/> <import file="${bu

我有一组构建文件,其中一些调用其他文件——首先导入它们。最终版本可能有或没有特定的目标(例如“copyother”)。如果目标是在行尾构建脚本中定义的,我想从主构建文件调用它。我怎么做

调用脚本的一部分:

<!-- Import project-specific libraries and classpath -->
<property name="build.dir" value="${projectDir}/build"/>
<import file="${build.dir}/build_libs.xml"/>

...

<!-- "copyother" is a foreign target, imported in build_libs.xml per project -->
<target name="pre-package" depends="    clean,
                                        init,
                                        compile-src,
                                        copy-src-resources,
                                        copy-app-resources,
                                        copyother,
                                        compile-tests,
                                        run-junit-tests"/>

...

我不希望每个项目都定义“copyother”目标。如何执行条件ant调用?

我猜您没有将“其他”构建脚本导入到main build.xml中。(因为这行不通。Ant将进口视为本地产品。)

同时,您使用的是depends,而不是ant/ant调用,因此您可能要导入它们,但一次导入一个

你不能在本地蚂蚁身上做你想做的事。正如您所指出的,测试一个文件很容易,但目标却不容易。尤其是如果另一个项目还没有加载。你必须编写一个定制的Ant任务来完成你想要的任务。两条途径:

1) 调用project.getTargets()并查看您的目标是否存在。这涉及到重构您的脚本以使用ant/antcall而不是纯依赖,但感觉不像是黑客。编写自定义Java条件并不难,Ant手册中有一个例子


2) 如果当前项目中还没有目标,请将其添加到当前项目中。新的目标将是禁止操作[不确定这种方法是否有效]

您应该探索使用1.7中添加到ANT中的条件。例如,您可以将其用于antcontrib的if任务,如下所示,但由于其工作方式,您必须检查macrodef而不是taskdef:

<if>
   <typefound name="some-macrodef"/>
<then>
   <some-macrodef/>
   </then>
</if>


这样,具有名为“some macro或taskdef”的宏定义的ant文件将被调用,没有宏定义的其他ant文件将不会出现错误。

同样完整。另一种方法是使用一些目标来检查目标

这里讨论的方法是:(vimil的帖子)。检查是使用scriptdef完成的。所以它和其他答案(珍妮·博亚斯基)并没有什么不同,但脚本很容易添加

<scriptdef name="hastarget" language="javascript">
    <attribute name="targetname"/>
    <attribute name="property"/>
    <![CDATA[
       var targetname = attributes.get("property");
       if(project.getTargets().containsKey(targetname)) {
            project.setProperty(attributes.get("property"), "true");
       }
     ]]>
</scriptdef>

<target name="check-and-call-exports">
    <hastarget targetname="exports" property="is-export-defined"/>
    <if>
        <isset property="is-export-defined"/>
        <then>
            <antcall target="exports"   if="is-export-defined"/>
        </then>
    </if>
</target>

<target name="target-that-may-run-exports-if-available" depends="check-and-call-exports">


类似问题位于。不类似。测试文件很容易。目标测试不需要。谢谢。事实上,方法2是当前的实现。但随着项目数量和授权目标数量的增长,这变得不切实际。下周我将使用方法1。经过一些考虑,方法2被认为是最简单的。谢谢